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) 2014-2017, 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_help, []). 36:- use_module(library(lists)). 37:- use_module(library(http/http_dispatch)). 38:- use_module(library(http/http_server_files)). 39:- use_module(library(http/http_json)). 40:- use_module(library(http/json)). 41 42/** <module> SWISH help system 43 44This module serves help information for SWISH. 45 46@tbd Serve SWI-Prolog Markdown files. 47*/ 48 49:- http_handler(swish(help), serve_files_in_directory(swish_help), 50 [id(help),prefix]). 51:- http_handler(swish(help_index), 52 help_index, [id(swish_help_index)]). 53 54user:file_search_path(swish_help, swish(web/help)). 55 56%% help_index(+Request) 57% 58% Get a list of registered help topics. Help topics are described 59% in a file swish_help('index.json'). 60 61help_index(_Request) :- 62 help_files(HelpIndex), 63 reply_json(HelpIndex). 64 65%% help_files(JSON:list) is det. 66% 67% JSON is a list of JSON dicts containing the keys below. The list 68% is composed from all *.html files in the search path 69% `swish_help`. 70% 71% - file:File 72% - title:String 73 74help_files(AllExamples) :- 75 findall(Index, 76 absolute_file_name(swish_help(.), Index, 77 [ access(read), 78 file_type(directory), 79 file_errors(fail), 80 solutions(all) 81 ]), 82 ExDirs), 83 maplist(index_json, ExDirs, JSON), 84 append(JSON, AllExamples). 85 86index_json(Dir, JSON) :- 87 directory_file_path(Dir, 'index.json', File), 88 access_file(File, read), !, 89 read_file_to_json(File, JSON). 90index_json(Dir, JSON) :- 91 string_concat(Dir, "/*.{html}", Pattern), 92 expand_file_name(Pattern, Files), 93 maplist(help_file_json, Files, JSON). 94 95read_file_to_json(File, JSON) :- 96 setup_call_cleanup( 97 open(File, read, In, [encoding(utf8)]), 98 json_read_dict(In, JSON), 99 close(In)). 100 101%% help_file_json(+Path, -JSON) is det. 102% 103% @tbd Beautify title from file-name (_ --> space, start 104% with capital, etc). 105 106help_file_json(Path, json{file:File, title:Base}) :- 107 file_base_name(Path, File), 108 file_name_extension(Base, _, File)