public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
From: John MacFarlane <jgm-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org>
To: pompez <martinsifrar11-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
	pandoc-discuss
	<pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
Subject: Re: Is there a way to change the way Pandoc parses HTML inside of markdown documents?
Date: Mon, 16 Aug 2021 15:08:54 -0700	[thread overview]
Message-ID: <yh480k1r6tt53d.fsf@johnmacfarlane.net> (raw)
In-Reply-To: <aae29ca7-60ca-4349-af03-939f0ac503efn-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>


I'm afraid you'll have to write some parsing code...

pompez <martinsifrar11-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:

> I'm starting out with Lua filters and apologize for this possibly already 
> answered question. You can also read this question on StackOverflow 
> <https://stackoverflow.com/questions/68809527/is-there-a-way-to-change-the-way-pandoc-parses-html-inside-of-markdown-documents>
> .
>
> I'm using Pandoc to convert markdown to HTML. My markdown files also 
> contain some raw HTML. In the examples, I'll be using `<mark>` and `<u>`.
>
> Let's say I want to change every `<mark>` to a `<u>` tag. We parse the 
> input as HTML and look at the AST.
>
> ```
> $ echo '<u>foo</u> & <mark>bar</mark>' | pandoc --from=html --to native
> [Plain [Underline [Str "foo"],Space,Str "&",Space,Span ("", ["mark"],[]) 
> [Str "bar"]]]
> ```
>
> On this structure, we can use a simple filter which replaces `Span` 
> elements representing the `<mark>` tag and replaces with `Underline` 
> elements.
>
> ```
> function Span(elem)
>     if elem.classes[1]:gmatch('mark') then
>         return pandoc.Underline(elem.content)
>     end
> end
> ```
>
> ```
> [Plain [Underline [Str "foo"],Space,Str "&",Space,Underline [Str "bar"]]]
> ```
>
> This is good. But if we parse the same input as markdown, we get a much 
> less convenient structure.
>
> ```
> $ echo '<u>foo</u> & <mark>bar</mark>' | pandoc --from=markdown+raw_html 
> --to native
> [Para [RawInline (Format "html") "<u>",Str "foo",RawInline (Format "html") 
> "</u>",Space,Str "&",Space,RawInline (Format "html") "<mark>",Str 
> "bar",RawInline (Format "html") "</mark>"]]
> ```
>
> And if we had some additional criteria by which to replace `<mark>` with 
> `<u>` (the content for example), we would have to identify the opening and 
> closing `RawInline` elements.
>
> I'm wondering if there is any good solutions to this problem? Is there a 
> way to parse HTML in markdown just as HTML would be parsed otherwise? Or is 
> there way to solve this in a Lua filter without writing some parsing code?
>
> -- 
> You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/aae29ca7-60ca-4349-af03-939f0ac503efn%40googlegroups.com.


  parent reply	other threads:[~2021-08-16 22:08 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-16 21:43 pompez
     [not found] ` <aae29ca7-60ca-4349-af03-939f0ac503efn-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2021-08-16 22:08   ` John MacFarlane [this message]
     [not found]     ` <yh480k1r6tt53d.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
2021-08-16 22:55       ` pompez
2021-08-17 10:37       ` William Lupton
     [not found]         ` <CAEe_xxj-kp22oToH4o5J54s16W4WzMkiaEicOy+TuqDZf5LP3g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2021-08-17 11:24           ` Bastien DUMONT
2021-08-24  8:44           ` pompez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=yh480k1r6tt53d.fsf@johnmacfarlane.net \
    --to=jgm-tvlzxgkolnx2fbvcvol8/a@public.gmane.org \
    --cc=martinsifrar11-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).