1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: jan@swi-prolog.org 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2023, SWI-Prolog Solutions b.v. 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(pldoc_markdown, 36 [ print_markdown/2 % +String, +Options 37 ]). 38:- use_module(library(pldoc)). 39:- autoload(library(pldoc/doc_html)). 40:- autoload(library(pldoc/doc_wiki), [wiki_codes_to_dom/3]). 41:- autoload(library(sgml), [load_html/3]). 42:- autoload(library(http/html_write), [html/3, print_html/1]). 43:- use_module(library(lynx/pldoc_style), []). 44:- autoload(library(lynx/html_text), [html_text/2]). 45 46:- predicate_options(print_markdown/2, 2, 47 [ pass_to(wiki_codes_to_dom/3, 2), 48 pass_to(load_html/3, 3), 49 pass_to(html_text/2, 2) 50 ]). 51 52/** <module> Markdown utilities 53 54This module provides high level PlDoc markdown utilities. 55 56@tbd The current implementation creates the markdown DOM, translates 57 this to HTML, then parses it and feeds it into the HTML 58 rendering. Creating an parsing the HTML string representation 59 could be skipped. 60*/ 61 62%! print_markdown(+Input, +Options) is det. 63% 64% Print a PlDoc markdown formatted String to `current_output` as 65% formatted text. 66% 67% @arg Input is either a term `Format-Args`, a atom, string, or a list 68% of codes. 69% @see html_text/2 for the most useful options. 70 71print_markdown(String, Options) :- 72 to_codes(String, Codes), 73 wiki_codes_to_dom(Codes, Options, DOM1), 74 phrase(html(DOM1), Tokens), 75 with_output_to(string(HTML), print_html(Tokens)), 76 setup_call_cleanup( 77 open_string(HTML, In), 78 load_html(In, DOM, Options), 79 close(In)), 80 html_text(DOM, Options). 81 82to_codes(Fmt-Args, Codes) => 83 format(codes(Codes), Fmt, Args). 84to_codes(String, Codes), atomic(String) => 85 string_codes(String, Codes). 86to_codes(List, Codes), is_of_type(List, Codes) => 87 Codes = List