diff options
author | Loïc Hoguin <[email protected]> | 2018-06-11 22:21:36 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2018-06-11 22:21:36 +0200 |
commit | dbb935a5972350f734d4abcf31c03f9e54bbd1d4 (patch) | |
tree | bbd4f56f9a5647c807187eb55f033255a29488df /src | |
parent | 976dfc5d92e3e23f356cb19f17ff51b22c75e634 (diff) | |
download | asciideck-dbb935a5972350f734d4abcf31c03f9e54bbd1d4.tar.gz asciideck-dbb935a5972350f734d4abcf31c03f9e54bbd1d4.tar.bz2 asciideck-dbb935a5972350f734d4abcf31c03f9e54bbd1d4.zip |
Add source-highlight support to HTML output
Diffstat (limited to 'src')
-rw-r--r-- | src/asciideck.erl | 1 | ||||
-rw-r--r-- | src/asciideck_source_highlight.erl | 27 | ||||
-rw-r--r-- | src/asciideck_to_html.erl | 16 | ||||
-rw-r--r-- | src/asciideck_transform_pass.erl | 33 |
4 files changed, 71 insertions, 6 deletions
diff --git a/src/asciideck.erl b/src/asciideck.erl index d962c1b..232ff32 100644 --- a/src/asciideck.erl +++ b/src/asciideck.erl @@ -46,6 +46,7 @@ parse(Data) -> parse(Data, _St) -> Passes = [ asciideck_attributes_pass, + asciideck_transform_pass, asciideck_lists_pass, asciideck_tables_pass, asciideck_inline_pass diff --git a/src/asciideck_source_highlight.erl b/src/asciideck_source_highlight.erl new file mode 100644 index 0000000..24c3054 --- /dev/null +++ b/src/asciideck_source_highlight.erl @@ -0,0 +1,27 @@ +%% Copyright (c) 2018, Loïc Hoguin <[email protected]> +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% https://www.gnu.org/software/src-highlite/source-highlight.html +-module(asciideck_source_highlight). + +-export([filter/2]). + +filter(Input, #{2 := Lang}) -> + TmpFile = "/tmp/asciideck-" ++ integer_to_list(erlang:phash2(make_ref())), + ok = file:write_file(TmpFile, Input), + Output = os:cmd(io_lib:format( + "source-highlight -i ~s -s ~s", + [TmpFile, Lang])), + file:delete(TmpFile), + unicode:characters_to_binary(Output). diff --git a/src/asciideck_to_html.erl b/src/asciideck_to_html.erl index c6fbbff..b904f3a 100644 --- a/src/asciideck_to_html.erl +++ b/src/asciideck_to_html.erl @@ -12,8 +12,6 @@ %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -%% @todo https://www.gnu.org/software/src-highlite/source-highlight.html - -module(asciideck_to_html). -export([translate/2]). @@ -96,7 +94,13 @@ paragraph({paragraph, _, Text, _}) -> %% Listing blocks. -listing_block({listing_block, Attrs, Listing, _}) -> +listing_block({listing_block, Attrs, Listing0, _}) -> + Listing = case Attrs of + #{1 := <<"source">>, 2 := _} -> + try asciideck_source_highlight:filter(Listing0, Attrs) catch C:E -> io:format("~p ~p ~p~n", [C, E, erlang:get_stacktrace()]), exit(bad) end; + _ -> + ["<pre>", html_encode(Listing0), "</pre>"] + end, [ "<div class=\"listingblock\">", case Attrs of @@ -105,9 +109,9 @@ listing_block({listing_block, Attrs, Listing, _}) -> _ -> [] end, - "<div class=\"content\"><pre>", - html_encode(Listing), - "</pre></div></div>\n" + "<div class=\"content\">", + Listing, + "</div></div>\n" ]. %% Lists. diff --git a/src/asciideck_transform_pass.erl b/src/asciideck_transform_pass.erl new file mode 100644 index 0000000..a1cf37c --- /dev/null +++ b/src/asciideck_transform_pass.erl @@ -0,0 +1,33 @@ +%% Copyright (c) 2018, Loïc Hoguin <[email protected]> +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +%% The purpose of this pass is to transform elements based +%% on the style given in their attributes. +-module(asciideck_transform_pass). + +-export([run/1]). + +run([]) -> + []; +%% The following syntax gets converted in the corresponding +%% listing_block element: +%% +%% [source,erlang] +%% f() -> ok. +%% +%% @todo We should not totally overwrite subs. +run([{paragraph, Attrs=#{1 := <<"source">>}, Text, Ann}|Tail]) -> + [{listing_block, Attrs#{<<"subs">> => <<"verbatim">>}, Text, Ann}|run(Tail)]; +run([Block|Tail]) -> + [Block|run(Tail)]. |