aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2018-06-11 22:21:36 +0200
committerLoïc Hoguin <[email protected]>2018-06-11 22:21:36 +0200
commitdbb935a5972350f734d4abcf31c03f9e54bbd1d4 (patch)
treebbd4f56f9a5647c807187eb55f033255a29488df
parent976dfc5d92e3e23f356cb19f17ff51b22c75e634 (diff)
downloadasciideck-dbb935a5972350f734d4abcf31c03f9e54bbd1d4.tar.gz
asciideck-dbb935a5972350f734d4abcf31c03f9e54bbd1d4.tar.bz2
asciideck-dbb935a5972350f734d4abcf31c03f9e54bbd1d4.zip
Add source-highlight support to HTML output
-rw-r--r--ebin/asciideck.app2
-rw-r--r--src/asciideck.erl1
-rw-r--r--src/asciideck_source_highlight.erl27
-rw-r--r--src/asciideck_to_html.erl16
-rw-r--r--src/asciideck_transform_pass.erl33
-rw-r--r--test/parser_SUITE.erl10
6 files changed, 82 insertions, 7 deletions
diff --git a/ebin/asciideck.app b/ebin/asciideck.app
index 7c85660..da6189e 100644
--- a/ebin/asciideck.app
+++ b/ebin/asciideck.app
@@ -1,7 +1,7 @@
{application, 'asciideck', [
{description, "Asciidoc for Erlang."},
{vsn, "0.2.0"},
- {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']},
+ {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']},
{registered, []},
{applications, [kernel,stdlib]},
{env, []}
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)].
diff --git a/test/parser_SUITE.erl b/test/parser_SUITE.erl
index 77b9f7a..e5eda88 100644
--- a/test/parser_SUITE.erl
+++ b/test/parser_SUITE.erl
@@ -275,6 +275,16 @@ listing_filter_source_title(_) ->
"----\n"),
ok.
+paragraph_filter_source(_) ->
+ doc("Source code listing filter as a paragraph. (source-highlight-filter)"),
+ Source = <<
+ "init(Req, State) ->\n"
+ " {ok, Req, State}.">>,
+ [{listing_block, #{1 := <<"source">>, 2 := <<"erlang">>}, Source, _}] = parse(iolist_to_binary([
+ "[source,erlang]\n",
+ Source, "\n"])),
+ ok.
+
%% Bulleted lists.
unordered_list(_) ->