View source with formatted 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	  [ wiki_file_codes_to_dom/3,		% +Code, +Files, -DOM
   37	    wiki_html//1			% :HTML
   38	  ]).   39:- use_module(library(http/http_dispatch)).   40:- use_module(library(http/http_parameters)).   41:- use_module(library(http/http_client)).   42:- use_module(library(http/html_write)).   43:- use_module(library(http/html_head)).   44:- use_module(library(pldoc/doc_html),
   45	      except([ file//2
   46		     ])).   47:- use_module(library(pldoc/doc_wiki)).   48:- use_module(library(option)).   49:- use_module(library(filesex)).   50
   51:- use_module(storage).   52:- use_module(config).   53
   54:- html_meta
   55	wiki_html(html,?,?).   56
   57:- multifile
   58	dom_expansion/2.   59
   60
   61/** <module> SWISH Notebook markdown support
   62
   63This module translates markdown cells for teh SWISH Notebook into HTML
   64*/
   65
   66:- http_handler(swish(markdown), markdown, [id(markdown)]).   67
   68%%	markdown(+Request)
   69%
   70%	Translate a Markdown text for the   SWISH  Notebook into an HTML
   71%	document.
   72
   73markdown(Request) :-
   74	option(method(get), Request), !,
   75        http_parameters(Request,
   76                        [ text(Data, [optional(true), default('')])
   77                        ]),
   78        atom_codes(Data, Codes),
   79        wiki_file_codes_to_dom(Codes, '/', DOM), % FIXME: What file to pass?
   80        phrase(html(DOM), Tokens),
   81        format('Content-type: text/html; charset=UTF-8\n\n'),
   82        print_html(Tokens).
   83markdown(Request) :-
   84	option(method(post), Request), !,
   85	http_read_data(Request, Codes, [to(codes)]),
   86	wiki_file_codes_to_dom(Codes, '/', DOM),
   87	phrase(html(DOM), Tokens),
   88        format('Content-type: text/html; charset=UTF-8\n\n'),
   89        print_html(Tokens).
   90
   91%%      wiki_codes_to_dom(+Codes, +File, -DOM)
   92%
   93%       DOM is the HTML dom representation for Codes that originate from
   94%       File.
   95
   96wiki_file_codes_to_dom(String, File, DOM) :-
   97        (   nb_current(pldoc_file, OrgFile)
   98        ->  setup_call_cleanup(
   99                b_setval(pldoc_file, File),
  100                wiki_codes_to_dom(String, [], DOM0),
  101                b_setval(pldoc_file, OrgFile))
  102        ;   setup_call_cleanup(
  103                b_setval(pldoc_file, File),
  104                wiki_codes_to_dom(String, [], DOM0),
  105                nb_delete(pldoc_file))
  106        ),
  107	expand_dom(DOM0, DOM).
  108
  109expand_dom(DOM0, DOM) :-
  110	dom_expansion(DOM0, DOM), !.
  111expand_dom(DOM, DOM).
  112
  113
  114		 /*******************************
  115		 *	     HOOK WIKI		*
  116		 *******************************/
  117
  118
  119wiki_html(_:HTML) -->
  120	html(swish_markdown:HTML).
  121
  122
  123:- multifile
  124	prolog:doc_autolink_extension/2.  125
  126prolog:doc_autolink_extension(swinb, notebook).
  127prolog:doc_autolink_extension(lnk,   permalink).
  128
  129:- public
  130	file//2.  131
  132%%	file(+File, +Options)//
  133%
  134%	Hook that deals with linking other notebooks using the following
  135%	markdown syntax:
  136%
  137%	  ```
  138%	  - [My first book](mybook.swinb)
  139%	  - [Label](store.pl)
  140%	  - [Label](library/lists.pl)
  141%	  ```
  142
  143:- multifile
  144	swish_config:source_alias/2.  145
  146file(File, Options) -->
  147	{ once(sub_atom(File, Pre, _, _Post, /)),
  148	  sub_atom(File, 0, Pre, _, Alias),
  149	  swish_config:source_alias(Alias, _Options),
  150	  option(label(Label), Options),
  151	  http_location_by_id(swish, Swish),
  152	  directory_file_path(Swish, File, HREF)
  153	}, !,
  154	html(a([class([alias,file]), href(HREF)], Label)).
  155file(File, Options) -->
  156	{ storage_file(File),
  157	  option(label(Label), Options, File),
  158	  http_location_by_id(swish, Swish),
  159	  directory_file_path(Swish, p, StoreDir),
  160	  directory_file_path(StoreDir, File, HREF)
  161	}, !,
  162	html(a([class(store), href(HREF)], Label)).
  163file(File, Options) -->
  164	pldoc_html:file(File, Options)