Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 4fadd44fa6
Fetching contributors…

Cannot retrieve contributors at this time

file 67 lines (62 sloc) 3.574 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
-module(jerome_bbcode_consumer).

-export([consume/2]).

-include("jerome.hrl").

consume(Binary, ImageFun) when is_binary(Binary), is_function(ImageFun) ->
    {ok, Tokens} = jerome_bbcode_scanner:scan(unicode:characters_to_list(Binary)),
    {ok, ParseTree} = jerome_bbcode_parser:parse(Tokens),
    process_tree(ParseTree, ImageFun).

process_tree(Tree, ImageFun) ->
    process_tree(Tree, [], #jerome_ctx{ image_fun = ImageFun }).

process_tree([], Acc, _) ->
    jerome:consolidate(lists:reverse(Acc));
process_tree([{bold, Elements}|Rest], Acc, Context) ->
    Ast = process_tree(Elements, [], Context#jerome_ctx{ bold = true }),
    process_tree(Rest, lists:reverse(Ast, Acc), Context);
process_tree([{italic, Elements}|Rest], Acc, Context) ->
    Ast = process_tree(Elements, [], Context#jerome_ctx{ italic = true }),
    process_tree(Rest, lists:reverse(Ast, Acc), Context);
process_tree([{underline, Elements}|Rest], Acc, Context) ->
    Ast = process_tree(Elements, [], Context#jerome_ctx{ underline = true }),
    process_tree(Rest, lists:reverse(Ast, Acc), Context);
process_tree([{superscript, Elements}|Rest], Acc, Context) ->
    Ast = process_tree(Elements, [], Context#jerome_ctx{ superscript = true }),
    process_tree(Rest, lists:reverse(Ast, Acc), Context);
process_tree([{subscript, Elements}|Rest], Acc, Context) ->
    Ast = process_tree(Elements, [], Context#jerome_ctx{ subscript = true }),
    process_tree(Rest, lists:reverse(Ast, Acc), Context);
process_tree([{hyperlink, {text, _, Value} = Token}|Rest], Acc, Context) ->
    Ast = process_tree([Token], [], Context#jerome_ctx{ hyperlink = Value }),
    process_tree(Rest, lists:reverse(Ast, Acc), Context);
process_tree([{hyperlink, {text, _, Value}, Elements}|Rest], Acc, Context) ->
    Ast = process_tree(Elements, [], Context#jerome_ctx{ hyperlink = Value }),
    process_tree(Rest, lists:reverse(Ast, Acc), Context);
process_tree([{image, {text, _, Value}}|Rest], Acc, Context) ->
    {ok, Image} = (Context#jerome_ctx.image_fun)(Value),
    process_tree(Rest, [{image, Image}|Acc], Context);
process_tree([{quote, Elements}|Rest], Acc, Context) ->
    Ast = process_tree(Elements, [], Context),
    process_tree(Rest, [{blockquote, Ast}|Acc], Context);
process_tree([{code, Elements}|Rest], Acc, Context) ->
    Ast = process_tree(Elements, [], Context),
    process_tree(Rest, [{blockcode, Ast}|Acc], Context);
process_tree([{list, Elements}|Rest], Acc, Context) ->
    Ast = process_tree(Elements, [], Context),
    process_tree(Rest, [{list, Ast}|Acc], Context);
process_tree([{list_item, Elements}|Rest], Acc, Context) ->
    Ast = process_tree(Elements, [], Context),
    process_tree(Rest, [{list_item, Ast}|Acc], Context);
process_tree([{table, Elements}|Rest], Acc, Context) ->
    Ast = process_tree(Elements, [], Context),
    process_tree(Rest, [{table, Ast}|Acc], Context);
process_tree([{table_row, Elements}|Rest], Acc, Context) ->
    Ast = process_tree(Elements, [], Context),
    process_tree(Rest, [{table_row, Ast}|Acc], Context);
process_tree([{table_cell, Elements}|Rest], Acc, Context) ->
    Ast = process_tree(Elements, [], Context),
    process_tree(Rest, [{table_cell, Ast}|Acc], Context);
process_tree([{text, _, Text}|Rest], Acc, Context) ->
    process_tree(Rest, [{text, Text, jerome:text_properties(Context)}|Acc], Context);
process_tree([{newline, _}, {newline, _}|Rest], Acc, Context) ->
    process_tree(Rest, [{paragraph, left}, {paragraph, left}|Acc], Context);
process_tree([{newline, _}|Rest], Acc, Context) ->
    process_tree(Rest, Acc, Context).
Something went wrong with that request. Please try again.