public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
From: Martin Post <martinpostberlin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: pandoc-discuss <pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
Subject: Re: Filter for automatic md > HTML block level element ID creation?
Date: Mon, 1 Aug 2022 13:30:48 -0700 (PDT)	[thread overview]
Message-ID: <22f277e9-5f3f-4673-a898-cf8be9e6492fn@googlegroups.com> (raw)
In-Reply-To: <15c136b0-7748-e24d-65a5-1072d34b7a04-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>


[-- Attachment #1.1: Type: text/plain, Size: 3697 bytes --]

Hi Jiří,

thank you (again :) for this filter. I can confirm it works, with the only 
bug I found so far being stand-alone images converted to regular ID’d 
paragraphs instead of HTML figures.

On Saturday, July 30, 2022 at 4:34:56 PM UTC+2 wol...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:

> (I wrote this message accidentally as a reply not to the mailing list, 
> so I send it once again to the list.)
>
> Below, you can get the source code of a Lua filter that adds anchors to 
> all paragraphs, list items (list items are also paragraphs in Pandoc) 
> and headings. The anchors are numbered (#para-0, #para-1 etc.).
>
> If you really need
>
> To the end of every paragraph (or other block), a link to the paragraph 
> is added.
>
> Both links and anchors are given a class name. You can style them. I 
> recommend this CSS:
>
> .para-link {
> font-size: .7em;
> text-decoration: none;
> color: inherit;
> opacity: .7;
> }
> .para-link:hover, .para-link:focus {
> text-decoration: underline;
> opacity: 1;
> }
>
>
> Usage of the filter:
>
> pandoc inputfile.md -o outputfile.html --lua-filter=blockids.lua
>
> (Where the file blockids.lua is the filter file. Save it to the data dir 
> subdirectory ‘filters/’ or the cwd.)
>
> The code follows.
>
>
> -- Adds IDs to every block of the document.
>
> -- MIT License
> -- 
> -- Copyright (c) 2022 Jiří Wolker
> -- 
> -- Permission is hereby granted, free of charge, to any person obtaining 
> a copy
> -- of this software and associated documentation files (the "Software"), 
> to deal
> -- in the Software without restriction, including without limitation the 
> rights
> -- to use, copy, modify, merge, publish, distribute, sublicense, and/or 
> sell
> -- copies of the Software, and to permit persons to whom the Software is
> -- furnished to do so, subject to the following conditions:
> -- 
> -- The above copyright notice and this permission notice shall be 
> included in
> -- all copies or substantial portions of the Software.
> -- 
> -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
> EXPRESS OR
> -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 
> SHALL THE
> -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
> ARISING FROM,
> -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
> DEALINGS IN THE
> -- SOFTWARE.
>
> paragraph_number = 0
>
> function Block(elem)
> local blocks_to_modify = {
> ["Para"] = true,
> ["Div"] = true,
> ["Header"] = true,
> }
> if blocks_to_modify[elem.t] then
> id = "para-"..paragraph_number
> paragraph_number = paragraph_number + 1
>
> anchor = pandoc.Link({ }, "#"..id)
> anchor.classes:insert("para-anchor")
> anchor.identifier = id
>
> link = pandoc.Link("link", "#"..id)
> link.classes:insert("para-link")
>
> -- At the start of the block:
> elem.content:insert(1, anchor)
>
> -- At the end of the block:
> elem.content:insert(pandoc.Space())
> elem.content:insert(link)
>
> -- Return the modified block.
> return elem
> end
> end
>

-- 
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/22f277e9-5f3f-4673-a898-cf8be9e6492fn%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 4731 bytes --]

      parent reply	other threads:[~2022-08-01 20:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-30 11:35 Martin Post
     [not found] ` <fe63819e-1816-4948-a675-a8fe85510e18n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2022-07-30 11:40   ` Jiří Wolker
2022-07-30 11:41   ` Jiří Wolker
     [not found]     ` <982ef1cb-79ef-8d82-f215-90b725e51613-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2022-07-30 12:08       ` Martin Post
     [not found]         ` <e9b07f81-df21-423d-bd01-0121b9428ddan-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2022-07-30 12:10           ` Jiří Wolker
2022-07-30 14:34   ` Jiří Wolker
     [not found]     ` <15c136b0-7748-e24d-65a5-1072d34b7a04-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2022-08-01 20:30       ` Martin Post [this message]

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=22f277e9-5f3f-4673-a898-cf8be9e6492fn@googlegroups.com \
    --to=martinpostberlin-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).