Add source-highlight support to HTML output
Loïc Hoguin
5 years ago
0 | 0 | {application, 'asciideck', [ |
1 | 1 | {description, "Asciidoc for Erlang."}, |
2 | 2 | {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']}, | |
4 | 4 | {registered, []}, |
5 | 5 | {applications, [kernel,stdlib]}, |
6 | 6 | {env, []} |
45 | 45 | parse(Data, _St) -> |
46 | 46 | Passes = [ |
47 | 47 | asciideck_attributes_pass, |
48 | asciideck_transform_pass, | |
48 | 49 | asciideck_lists_pass, |
49 | 50 | asciideck_tables_pass, |
50 | 51 | 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). |
10 | 10 | %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
11 | 11 | %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
12 | 12 | %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
13 | ||
14 | %% @todo https://www.gnu.org/software/src-highlite/source-highlight.html | |
15 | 13 | |
16 | 14 | -module(asciideck_to_html). |
17 | 15 | |
95 | 93 | |
96 | 94 | %% Listing blocks. |
97 | 95 | |
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, | |
99 | 103 | [ |
100 | 104 | "<div class=\"listingblock\">", |
101 | 105 | case Attrs of |
104 | 108 | _ -> |
105 | 109 | [] |
106 | 110 | 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" | |
110 | 114 | ]. |
111 | 115 | |
112 | 116 | %% 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)]. |
272 | 272 | "----\n" |
273 | 273 | "1 = 2.\n" |
274 | 274 | "----\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"])), | |
275 | 285 | ok. |
276 | 286 | |
277 | 287 | %% Bulleted lists. |