%---------------------------------------------------------------------------% % vim: ft=mercury ts=4 sw=4 et %---------------------------------------------------------------------------% % Copyright (C) 2017-2018 The Mercury team. % This file is distributed under the terms specified in COPYING.LIB. %---------------------------------------------------------------------------% % % File: term_to_html.m. % Main author: wangp. % % This module produces an HTML document for browsing a Mercury term. % %---------------------------------------------------------------------------% %---------------------------------------------------------------------------% :- module mdb.term_to_html. :- interface. :- import_module io. :- import_module maybe. :- import_module mdb.browser_term. :- pred write_html_doc(io.output_stream::in, browser_term::in, string::in, maybe_error::out, io::di, io::uo) is cc_multi. %---------------------------------------------------------------------------% %---------------------------------------------------------------------------% :- implementation. :- import_module bool. :- import_module char. :- import_module construct. :- import_module deconstruct. :- import_module dir. :- import_module exception. :- import_module int. :- import_module list. :- import_module string. :- import_module type_desc. :- import_module univ. :- import_module mdb.browse. :- import_module mdb.percent_encoding. %---------------------------------------------------------------------------% write_html_doc(Stream, BrowserTerm, MdbDir, Result, !IO) :- try_io(write_html_doc_2(Stream, BrowserTerm, MdbDir), TryResult, !IO), ( TryResult = succeeded({}), Result = ok ; TryResult = exception(Univ), ( if univ_to_type(Univ, Error : io.error) then Result = error(io.error_message(Error)) else Result = error(string(univ_value(Univ))) ) ). :- pred write_html_doc_2(io.output_stream::in, browser_term::in, string::in, {}::out, io::di, io::uo) is cc_multi. write_html_doc_2(Stream, BrowserTerm, MdbDir, {}, !IO) :- make_file_url_prefix(MdbDir, FilePrefix), list.foldl(write_string(Stream), header(FilePrefix), !IO), write_browser_term_in_script(Stream, BrowserTerm, !IO), io.write_string(Stream, footer, !IO). :- pred make_file_url_prefix(string::in, string::out) is det. make_file_url_prefix(Path0, FilePrefix) :- % Replace backslashes with forward slashes in Windows paths. ( if dir.directory_separator('\\') then string.replace_all(Path0, "\\", "/", Path) else Path = Path0 ), Segments = string.split_at_char('/', Path), EncodedSegments = list.map(percent_encode_path_segment, Segments), EncodedPath = string.join_list("/", EncodedSegments), ( if string.prefix(EncodedPath, "/") then FilePrefix = "file://" ++ EncodedPath else FilePrefix = "file:///" ++ EncodedPath ). :- func header(string) = list(string). header(FilePrefix) = [ "\n", "\n", "\n", "\n", "\n", "\n", "