View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@cs.vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (C): 2013, VU University Amsterdam
    7
    8    This program is free software; you can redistribute it and/or
    9    modify it under the terms of the GNU General Public License
   10    as published by the Free Software Foundation; either version 2
   11    of the License, or (at your option) any later version.
   12
   13    This program is distributed in the hope that it will be useful,
   14    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16    GNU General Public License for more details.
   17
   18    You should have received a copy of the GNU General Public
   19    License along with this library; if not, write to the Free Software
   20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21
   22    As a special exception, if you link this library with other files,
   23    compiled with a Free Software compiler, to produce an executable, this
   24    library does not by itself cause the resulting executable to be covered
   25    by the GNU General Public License. This exception does not however
   26    invalidate any other reasons why the executable file might be covered by
   27    the GNU General Public License.
   28*/
   29
   30:- module(markitup,
   31	  [ markitup//1
   32	  ]).   33:- use_module(library(http/http_dispatch)).   34:- use_module(library(http/html_head)).   35:- use_module(library(http/http_parameters)).   36:- use_module(library(http/html_write)).   37:- use_module(library(http/js_write)).   38:- use_module(library(option)).   39:- use_module(markdown).   40:- use_module(wiki).   41
   42/** <module> Wrapper for markItUp ajax markup editor
   43
   44@see http://markitup.jaysalvat.com/home/
   45*/
   46
   47:- http_handler(root('markitup/preview/markdown'), preview_markdown, []).   48:- http_handler(root('markitup/preview/pldoc'),    preview_pldoc,    []).   49
   50:- html_resource(js('markitup/jquery.markitup.js'),
   51		 [ requires([ jquery
   52			    ])
   53		 ]).   54:- html_resource(js('markitup/sets/markdown/set.js'),
   55		 [ requires([ js('markitup/jquery.markitup.js'),
   56			      js('markitup/skins/markitup/style.css'),
   57			      js('markitup/sets/markdown/style.css')
   58			    ])
   59		 ]).   60:- html_resource(markdown,
   61		 [ virtual(true),
   62		   requires([ js('markitup/sets/markdown/set.js')
   63			    ])
   64		 ]).   65:- html_resource(js('markitup/sets/pldoc/set.js'),
   66		 [ requires([ js('markitup/jquery.markitup.js'),
   67			      js('markitup/skins/markitup/style.css'),
   68			      js('markitup/sets/pldoc/style.css')
   69			    ])
   70		 ]).   71:- html_resource(pldoc,
   72		 [ virtual(true),
   73		   requires([ js('markitup/sets/pldoc/set.js')
   74			    ])
   75		 ]).   76
   77%%	markitup(Options)// is det.
   78%
   79%	Insert a =textarea= with markItUp support.
   80
   81markitup(Options) -->
   82	{ option(markup(Language), Options, markdown),
   83	  option(id(Id), Options, markdown),
   84	  option(name(Name), Options, Id),
   85	  option(cols(Cols), Options, 80),
   86	  option(rows(Rows), Options, 20),
   87	  option(value(Content), Options, []),
   88	  option(preview(Preview), Options, false)
   89	},
   90	html_requires(Language),
   91	html(textarea([id(Id), name(Name), cols(Cols), rows(Rows)], Content)),
   92	js_script({|javascript(Id,Language,Preview)||
   93		   $(document).ready(function() {
   94		      $("#"+Id).markItUp(eval(Language+"_settings"));
   95		      if ( eval(Preview) ) {
   96			$('a[title="Preview"]').trigger("mouseup");
   97		      }
   98		    });
   99		  |}).
  100
  101
  102%%	preview_markdown(+Request)
  103%
  104%	Handle preview requests from markItUp.  The data is send using
  105%	a POST request, where the =data= field contains the content of
  106%	the textarea.
  107
  108preview_markdown(Request) :-
  109	http_parameters(Request,
  110			[ data(Data, [optional(true), default('')])
  111			]),
  112	debug(markitup(preview), 'Preview:~n~w~n', [Data]),
  113	open_atom_stream(Data, In),
  114	markdown_dom(stream(In), DOM),
  115	phrase(html(DOM), Tokens),
  116	format('Content-type: text/html; charset=UTF-8\n\n'),
  117	print_html(Tokens).
  118
  119%%	preview_pldoc(+Request)
  120%
  121%	Handle preview requests from markItUp.  The data is send using
  122%	a POST request, where the =data= field contains the content of
  123%	the textarea.
  124
  125preview_pldoc(Request) :-
  126	http_parameters(Request,
  127			[ data(Data, [optional(true), default('')])
  128			]),
  129	debug(markitup(preview), 'Preview:~n~w~n', [Data]),
  130	atom_codes(Data, Codes),
  131	wiki_file_codes_to_dom(Codes, '/', DOM), % FIXME: What file to pass?
  132	phrase(page(plain, [], [\html_requires(pldoc)|DOM]), Tokens),
  133	format('Content-type: text/html; charset=UTF-8\n\n'),
  134	print_html(Tokens).
  135
  136
  137		 /*******************************
  138		 *	       UTIL		*
  139		 *******************************/
  140
  141open_atom_stream(Atom, Stream) :-
  142	atom_to_memory_file(Atom, MF),
  143	open_memory_file(MF, read, Stream,
  144			 [ free_on_close(true)
  145			 ])