1:- module(md_span_decorate, [
    2    md_span_decorate//2 % -Span
    3]).

Parser for span-level styles

Predicates for recognizing span-level style formatting like strong, emphasis and code. */

   12:- use_module(library(dcg/basics)).   13:- use_module(md_trim).
 md_span_decorate(-Span, +Allow)// is det
Recognizes style formatting in the middle of span text. Span is a term functor(Codes) where the functor is one of: strong, em or code. Allow is a list of allowed span elements. May contain strong, em, del and code.
   27md_span_decorate(Span, Allow) -->
   28    { memberchk(strong, Allow) },
   29    star_strong(Span), !.
   30
   31md_span_decorate(Span, Allow) -->
   32    { memberchk(strong, Allow) },
   33    underscore_strong(Span), !.
   34
   35md_span_decorate(Span, Allow) -->
   36    { memberchk(em, Allow) },
   37    star_emphasis(Span), !.
   38
   39md_span_decorate(Span, Allow) -->
   40    { memberchk(em, Allow) },
   41    underscore_emphasis(Span), !.
   42
   43md_span_decorate(Span, Allow) -->
   44    { memberchk(code, Allow) },
   45    code(Span).
   46
   47md_span_decorate(Span, Allow) -->
   48    { memberchk(del, Allow) },
   49    strikethrough(Span).
   50
   51% Recognizes strikethrough ~~something~~.
   52
   53strikethrough(del(Codes)) -->
   54    "~~", string(Codes), "~~".
   55
   56% Recognizes strong **something**.
   57
   58star_strong(strong(Codes)) -->
   59    "**", string(Codes), "**".
   60
   61% Recognizes strong __something__.
   62
   63underscore_strong(strong(Codes)) -->
   64    "__", string(Codes), "__".
   65
   66% Recognizes emhasis *something*.
   67% The first character following * must be a non-space.
   68
   69star_emphasis(em([Code|Codes])) -->
   70    "*", nonblank(Code), string(Codes), "*".
   71
   72% Recognizes emphasis _something_.
   73% The first character following _ must be a non-space.
   74
   75underscore_emphasis(em([Code|Codes])) -->
   76    "_", nonblank(Code), string(Codes), "_".
   77
   78% Recognizes inline code ``code``.
   79
   80code(code(Trimmed)) -->
   81    "``", string(Raw), "``",
   82    { trim(Raw, Trimmed) }.
   83
   84% Recognizes inline code `code`.
   85
   86code(code(Trimmed)) -->
   87    "`", string(Raw), "`",
   88    { trim(Raw, Trimmed) }