Codebase list erlang-asciideck / dbb935a
Add source-highlight support to HTML output Loïc Hoguin 5 years ago
6 changed file(s) with 82 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
00 {application, 'asciideck', [
11 {description, "Asciidoc for Erlang."},
22 {vsn, "0.2.0"},
3 {modules, ['asciideck','asciideck_attributes_parser','asciideck_attributes_pass','asciideck_block_parser','asciideck_inline_pass','asciideck_line_reader','asciideck_lists_pass','asciideck_reader','asciideck_stdin_reader','asciideck_tables_pass','asciideck_to_html','asciideck_to_manpage']},
3 {modules, ['asciideck','asciideck_attributes_parser','asciideck_attributes_pass','asciideck_block_parser','asciideck_inline_pass','asciideck_line_reader','asciideck_lists_pass','asciideck_reader','asciideck_source_highlight','asciideck_stdin_reader','asciideck_tables_pass','asciideck_to_html','asciideck_to_manpage','asciideck_transform_pass']},
44 {registered, []},
55 {applications, [kernel,stdlib]},
66 {env, []}
4545 parse(Data, _St) ->
4646 Passes = [
4747 asciideck_attributes_pass,
48 asciideck_transform_pass,
4849 asciideck_lists_pass,
4950 asciideck_tables_pass,
5051 asciideck_inline_pass
0 %% Copyright (c) 2018, Loïc Hoguin <essen@ninenines.eu>
1 %%
2 %% Permission to use, copy, modify, and/or distribute this software for any
3 %% purpose with or without fee is hereby granted, provided that the above
4 %% copyright notice and this permission notice appear in all copies.
5 %%
6 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
7 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
8 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
9 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
10 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
11 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
12 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
13
14 %% https://www.gnu.org/software/src-highlite/source-highlight.html
15 -module(asciideck_source_highlight).
16
17 -export([filter/2]).
18
19 filter(Input, #{2 := Lang}) ->
20 TmpFile = "/tmp/asciideck-" ++ integer_to_list(erlang:phash2(make_ref())),
21 ok = file:write_file(TmpFile, Input),
22 Output = os:cmd(io_lib:format(
23 "source-highlight -i ~s -s ~s",
24 [TmpFile, Lang])),
25 file:delete(TmpFile),
26 unicode:characters_to_binary(Output).
1010 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1111 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1212 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
13
14 %% @todo https://www.gnu.org/software/src-highlite/source-highlight.html
1513
1614 -module(asciideck_to_html).
1715
9593
9694 %% Listing blocks.
9795
98 listing_block({listing_block, Attrs, Listing, _}) ->
96 listing_block({listing_block, Attrs, Listing0, _}) ->
97 Listing = case Attrs of
98 #{1 := <<"source">>, 2 := _} ->
99 try asciideck_source_highlight:filter(Listing0, Attrs) catch C:E -> io:format("~p ~p ~p~n", [C, E, erlang:get_stacktrace()]), exit(bad) end;
100 _ ->
101 ["<pre>", html_encode(Listing0), "</pre>"]
102 end,
99103 [
100104 "<div class=\"listingblock\">",
101105 case Attrs of
104108 _ ->
105109 []
106110 end,
107 "<div class=\"content\"><pre>",
108 html_encode(Listing),
109 "</pre></div></div>\n"
111 "<div class=\"content\">",
112 Listing,
113 "</div></div>\n"
110114 ].
111115
112116 %% Lists.
0 %% Copyright (c) 2018, Loïc Hoguin <essen@ninenines.eu>
1 %%
2 %% Permission to use, copy, modify, and/or distribute this software for any
3 %% purpose with or without fee is hereby granted, provided that the above
4 %% copyright notice and this permission notice appear in all copies.
5 %%
6 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
7 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
8 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
9 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
10 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
11 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
12 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
13
14 %% The purpose of this pass is to transform elements based
15 %% on the style given in their attributes.
16 -module(asciideck_transform_pass).
17
18 -export([run/1]).
19
20 run([]) ->
21 [];
22 %% The following syntax gets converted in the corresponding
23 %% listing_block element:
24 %%
25 %% [source,erlang]
26 %% f() -> ok.
27 %%
28 %% @todo We should not totally overwrite subs.
29 run([{paragraph, Attrs=#{1 := <<"source">>}, Text, Ann}|Tail]) ->
30 [{listing_block, Attrs#{<<"subs">> => <<"verbatim">>}, Text, Ann}|run(Tail)];
31 run([Block|Tail]) ->
32 [Block|run(Tail)].
272272 "----\n"
273273 "1 = 2.\n"
274274 "----\n"),
275 ok.
276
277 paragraph_filter_source(_) ->
278 doc("Source code listing filter as a paragraph. (source-highlight-filter)"),
279 Source = <<
280 "init(Req, State) ->\n"
281 " {ok, Req, State}.">>,
282 [{listing_block, #{1 := <<"source">>, 2 := <<"erlang">>}, Source, _}] = parse(iolist_to_binary([
283 "[source,erlang]\n",
284 Source, "\n"])),
275285 ok.
276286
277287 %% Bulleted lists.