View source with raw comments or as raw
    1/*  Part of SWISH
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2015, VU University Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(swish_markdown, []).   36:- use_module(library(http/http_dispatch)).   37:- use_module(library(http/http_parameters)).   38:- use_module(library(http/http_client)).   39:- use_module(library(http/html_write)).   40:- use_module(library(http/html_head)).   41:- use_module(library(pldoc/doc_html),
   42	      except([ file//2
   43		     ])).   44:- use_module(library(pldoc/doc_wiki)).   45:- use_module(library(option)).   46:- use_module(library(filesex)).   47
   48:- use_module(storage).   49:- use_module(config).

SWISH Notebook markdown support

This module translates markdown cells for teh SWISH Notebook into HTML */

   56:- http_handler(swish(markdown), markdown, [id(markdown)]).
 markdown(+Request)
Translate a Markdown text for the SWISH Notebook into an HTML document.
   63markdown(Request) :-
   64	option(method(get), Request), !,
   65        http_parameters(Request,
   66                        [ text(Data, [optional(true), default('')])
   67                        ]),
   68        atom_codes(Data, Codes),
   69        wiki_file_codes_to_dom(Codes, '/', DOM), % FIXME: What file to pass?
   70        phrase(html(DOM), Tokens),
   71        format('Content-type: text/html; charset=UTF-8\n\n'),
   72        print_html(Tokens).
   73markdown(Request) :-
   74	option(method(post), Request), !,
   75	http_read_data(Request, Codes, [to(codes)]),
   76	wiki_file_codes_to_dom(Codes, '/', DOM),
   77	phrase(html(DOM), Tokens),
   78        format('Content-type: text/html; charset=UTF-8\n\n'),
   79        print_html(Tokens).
 wiki_codes_to_dom(+Codes, +File, -DOM)
DOM is the HTML dom representation for Codes that originate from File.
   86wiki_file_codes_to_dom(String, File, DOM) :-
   87        (   nb_current(pldoc_file, OrgFile)
   88        ->  setup_call_cleanup(
   89                b_setval(pldoc_file, File),
   90                wiki_codes_to_dom(String, [], DOM),
   91                b_setval(pldoc_file, OrgFile))
   92        ;   setup_call_cleanup(
   93                b_setval(pldoc_file, File),
   94                wiki_codes_to_dom(String, [], DOM),
   95                nb_delete(pldoc_file))
   96        ).
   97
   98
   99		 /*******************************
  100		 *	     HOOK WIKI		*
  101		 *******************************/
  102
  103:- multifile
  104	prolog:doc_autolink_extension/2.  105
  106prolog:doc_autolink_extension(swinb, notebook).
  107
  108:- public
  109	file//2.
 file(+File, +Options)//
Hook that deals with linking other notebooks using the following markdown syntax:
- [My first book](mybook.swinb)
- [Label](store.pl)
- [Label](library/lists.pl)
  122:- multifile
  123	swish_config:source_alias/2.  124
  125file(File, Options) -->
  126	{ once(sub_atom(File, Pre, _, _Post, /)),
  127	  sub_atom(File, 0, Pre, _, Alias),
  128	  swish_config:source_alias(Alias, _Options),
  129	  option(label(Label), Options),
  130	  http_location_by_id(swish, Swish),
  131	  directory_file_path(Swish, File, HREF)
  132	}, !,
  133	html(a([class([alias,file]), href(HREF)], Label)).
  134file(File, Options) -->
  135	{ storage_file(File),
  136	  option(label(Label), Options),
  137	  http_location_by_id(swish, Swish),
  138	  directory_file_path(Swish, p, StoreDir),
  139	  directory_file_path(StoreDir, File, HREF)
  140	}, !,
  141	html(a([class(store), href(HREF)], Label)).
  142file(File, Options) -->
  143	pldoc_html:file(File, Options)