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(library(debug)).   40:- use_module(markdown).   41:- use_module(wiki).   42
   43/** <module> Wrapper for markItUp ajax markup editor
   44
   45@see http://markitup.jaysalvat.com/home/
   46*/
   47
   48:- http_handler(root('markitup/preview/markdown'), preview_markdown, []).   49:- http_handler(root('markitup/preview/pldoc'),    preview_pldoc,    []).   50
   51:- html_resource(js('markitup/jquery.markitup.js'),
   52		 [ requires([ jquery
   53			    ])
   54		 ]).   55:- html_resource(js('markitup/sets/markdown/set.js'),
   56		 [ requires([ js('markitup/jquery.markitup.js'),
   57			      js('markitup/skins/markitup/style.css'),
   58			      js('markitup/sets/markdown/style.css')
   59			    ])
   60		 ]).   61:- html_resource(markdown,
   62		 [ virtual(true),
   63		   requires([ js('markitup/sets/markdown/set.js')
   64			    ])
   65		 ]).   66:- html_resource(js('markitup/sets/pldoc/set.js'),
   67		 [ requires([ js('markitup/jquery.markitup.js'),
   68			      js('markitup/skins/markitup/style.css'),
   69			      js('markitup/sets/pldoc/style.css')
   70			    ])
   71		 ]).   72:- html_resource(pldoc,
   73		 [ virtual(true),
   74		   requires([ js('markitup/sets/pldoc/set.js')
   75			    ])
   76		 ]).   77
   78%%	markitup(Options)// is det.
   79%
   80%	Insert a =textarea= with markItUp support.
   81
   82markitup(Options) -->
   83	{ option(markup(Language), Options, markdown),
   84	  option(id(Id), Options, markdown),
   85	  option(name(Name), Options, Id),
   86	  option(cols(Cols), Options, 80),
   87	  option(rows(Rows), Options, 20),
   88	  option(value(Content), Options, []),
   89	  option(preview(Preview), Options, false)
   90	},
   91	html_requires(Language),
   92	html(textarea([id(Id), name(Name), cols(Cols), rows(Rows)], Content)),
   93	js_script({|javascript(Id,Language,Preview)||
   94		   $(document).ready(function() {
   95		      $("#"+Id).markItUp(eval(Language+"_settings"));
   96		      if ( eval(Preview) ) {
   97			$('a[title="Preview"]').trigger("mouseup");
   98		      }
   99		    });
  100		  |}).
  101
  102
  103%%	preview_markdown(+Request)
  104%
  105%	Handle preview requests from markItUp.  The data is send using
  106%	a POST request, where the =data= field contains the content of
  107%	the textarea.
  108
  109preview_markdown(Request) :-
  110	http_parameters(Request,
  111			[ data(Data, [optional(true), default('')])
  112			]),
  113	debug(markitup(preview), 'Preview:~n~w~n', [Data]),
  114	open_atom_stream(Data, In),
  115	markdown_dom(stream(In), DOM),
  116	phrase(html(DOM), Tokens),
  117	format('Content-type: text/html; charset=UTF-8\n\n'),
  118	print_html(Tokens).
  119
  120%%	preview_pldoc(+Request)
  121%
  122%	Handle preview requests from markItUp.  The data is send using
  123%	a POST request, where the =data= field contains the content of
  124%	the textarea.
  125
  126preview_pldoc(Request) :-
  127	http_parameters(Request,
  128			[ data(Data, [optional(true), default('')])
  129			]),
  130	debug(markitup(preview), 'Preview:~n~w~n', [Data]),
  131	atom_codes(Data, Codes),
  132	wiki_file_codes_to_dom(Codes, '/', DOM), % FIXME: What file to pass?
  133	phrase(page(plain, [], [\html_requires(pldoc)|DOM]), Tokens),
  134	format('Content-type: text/html; charset=UTF-8\n\n'),
  135	print_html(Tokens).
  136
  137
  138		 /*******************************
  139		 *	       UTIL		*
  140		 *******************************/
  141
  142open_atom_stream(Atom, Stream) :-
  143	atom_to_memory_file(Atom, MF),
  144	open_memory_file(MF, read, Stream,
  145			 [ free_on_close(true)
  146			 ])