public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* Filter help
@ 2020-02-04  4:30 Alan Tyree
       [not found] ` <CAGMsgMwKsDspX=NfvqNNr1ZQMqLJOsYrGWHiMwBNwrZkNMiJXg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Alan Tyree @ 2020-02-04  4:30 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

[-- Attachment #1: Type: text/plain, Size: 1675 bytes --]

Early last year, "tarleb" posted a lua filter which allowed internal
references to appear as section numbers, for example, "see 2.1.2". The
filter appears below, and uses "hierarchicalize" which, since version 2.8,
must be rewritten with 'make_sections'.

Can someone please direct me as to how that is done? Many thanks,
Alan

---------------------------------
local hierarchicalize = (require 'pandoc.utils').hierarchicalize

local section_numbers = {}

function populate_section_numbers (doc)
  function populate (elements)
    for _, el in pairs(elements) do
      if el.t == 'Sec' then
        section_numbers['#' .. el.attr.identifier] =
table.concat(el.numbering, '.')
        populate(el.contents)
      end
    end
  end

  populate(hierarchicalize(doc.blocks))
end

function resolve_section_ref (link)
  if #link.content > 0 or link.target:sub(1, 1) ~= '#' then
    return nil
  end
  local section_number = pandoc.Str(section_numbers[link.target])
  return pandoc.Link({section_number}, link.target, link.title, link.attr)
end

return {
  {Pandoc = populate_section_numbers},
  {Link = resolve_section_ref}
}

-------------------------------------------------------




-- 
Alan L Tyree                    http://www2.austlii.edu.au/~alan

-- 
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/CAGMsgMwKsDspX%3DNfvqNNr1ZQMqLJOsYrGWHiMwBNwrZkNMiJXg%40mail.gmail.com.

[-- Attachment #2: Type: text/html, Size: 2607 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filter help
       [not found] ` <CAGMsgMwKsDspX=NfvqNNr1ZQMqLJOsYrGWHiMwBNwrZkNMiJXg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2020-02-05  3:37   ` EBkysko
       [not found]     ` <950405b0-1c8e-4842-8e1e-4d6e7fb6a909-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: EBkysko @ 2020-02-05  3:37 UTC (permalink / raw)
  To: pandoc-discuss


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


The original filter you're referring to was posted in: 
https://stackoverflow.com/a/54130482

Nowadays the equivalent would be almost the same, something like:

---------------------------------------------
-- for pandoc 2.9+

local make_sections = pandoc.utils.make_sections

local section_numbers = {}

function populate_section_numbers (doc)
  function populate(elements)
    for _,el in pairs(elements) do
      if el.t == "Div" and el.classes:includes("section") then
        section_numbers["#" .. el.identifier] = el.attributes.number
        populate(el.content)
      end
    end
  end

  populate(make_sections(true, 1, doc.blocks))  -- number_sections true, 
base_level 1
end

function resolve_section_ref (link)
  if #link.content > 0 or link.target:sub(1, 1) ~= '#' then
    return nil
  end
  table.insert(link.content, pandoc.Str(section_numbers[link.target]))
  return link
end

return {
  {Pandoc = populate_section_numbers},
  {Link = resolve_section_ref},
}

---------------------------------------------


Notes:

1) It is admissible to have whole sections enclosed within (non section-) 
Div's, but the above won't go within. You then have to use this more 
general function instead:


---------------------------------------------

function populate_section_numbers_div (doc)
  function populate(elements)
    for _,el in pairs(elements) do
      if el.t == "Div" then
        if el.classes:includes("section") then
          section_numbers["#" .. el.identifier] = el.attributes.number
        end
        populate(el.content)
      end
    end
  end

  populate(make_sections(true, 1, doc.blocks))
end

---------------------------------------------

and rename the function in the returned filter accordingly.


2) None of the above treat headers with class .unnumbered.


-- 
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/950405b0-1c8e-4842-8e1e-4d6e7fb6a909%40googlegroups.com.

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filter help
       [not found]     ` <950405b0-1c8e-4842-8e1e-4d6e7fb6a909-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2020-02-06  0:24       ` Alan Tyree
       [not found]         ` <CAGMsgMxdyS5VkAib7bPEoH7MtQPHX13CSwgh56B64CzPCT8oWg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Alan Tyree @ 2020-02-06  0:24 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

[-- Attachment #1: Type: text/plain, Size: 4195 bytes --]

Thanks for your help with this. My knowledge of lua is nil (whatever 'nil'
might be in lua).

Unfortunately, it didn't work for me.  I am on Manjaro Linux,  Pandoc
version 2.8. The command line that I am using is (all on one line, of
course):

"pandoc --to html5 --standalone --section-divs --number-sections --filter
pandoc-citeproc --lua-filter ./sec-no-filter-2.lua -o abl.html abl.yaml
abl.md"

A sample from the manuscript:

"This recommendation has been implemented by amendments to the Banking Act
1959: see [](#org1de4b9b)."

The target is:

"## Banking Act 1959 {#org1de4b9b}"

The target becomes "2.3 Banking Act 1959" in both html and docx. Obviously,
I would like the sample to translate to the publishers preferred form:

"This recommendation has been implemented by amendments to the Banking Act
1959: see 2.3."

It all works in pandoc version 2.5 and the original filter, but I am
getting  "see ." in the html with your revised filter (either the main one
or with the substituted function) under version 2.8.

Thanks again for your help, and any further would be greatly appreciated.
Cheers,
Alan


On Wed, 5 Feb 2020 at 14:37, EBkysko <ebkysko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:

>
> The original filter you're referring to was posted in:
> https://stackoverflow.com/a/54130482
>
> Nowadays the equivalent would be almost the same, something like:
>
> ---------------------------------------------
> -- for pandoc 2.9+
>
> local make_sections = pandoc.utils.make_sections
>
> local section_numbers = {}
>
> function populate_section_numbers (doc)
>   function populate(elements)
>     for _,el in pairs(elements) do
>       if el.t == "Div" and el.classes:includes("section") then
>         section_numbers["#" .. el.identifier] = el.attributes.number
>         populate(el.content)
>       end
>     end
>   end
>
>   populate(make_sections(true, 1, doc.blocks))  -- number_sections true,
> base_level 1
> end
>
> function resolve_section_ref (link)
>   if #link.content > 0 or link.target:sub(1, 1) ~= '#' then
>     return nil
>   end
>   table.insert(link.content, pandoc.Str(section_numbers[link.target]))
>   return link
> end
>
> return {
>   {Pandoc = populate_section_numbers},
>   {Link = resolve_section_ref},
> }
>
> ---------------------------------------------
>
>
> Notes:
>
> 1) It is admissible to have whole sections enclosed within (non section-)
> Div's, but the above won't go within. You then have to use this more
> general function instead:
>
>
> ---------------------------------------------
>
> function populate_section_numbers_div (doc)
>   function populate(elements)
>     for _,el in pairs(elements) do
>       if el.t == "Div" then
>         if el.classes:includes("section") then
>           section_numbers["#" .. el.identifier] = el.attributes.number
>         end
>         populate(el.content)
>       end
>     end
>   end
>
>   populate(make_sections(true, 1, doc.blocks))
> end
>
> ---------------------------------------------
>
> and rename the function in the returned filter accordingly.
>
>
> 2) None of the above treat headers with class .unnumbered.
>
>
> --
> 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/950405b0-1c8e-4842-8e1e-4d6e7fb6a909%40googlegroups.com
> <https://groups.google.com/d/msgid/pandoc-discuss/950405b0-1c8e-4842-8e1e-4d6e7fb6a909%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Alan L Tyree                    http://www2.austlii.edu.au/~alan

-- 
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/CAGMsgMxdyS5VkAib7bPEoH7MtQPHX13CSwgh56B64CzPCT8oWg%40mail.gmail.com.

[-- Attachment #2: Type: text/html, Size: 16786 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filter help
       [not found]         ` <CAGMsgMxdyS5VkAib7bPEoH7MtQPHX13CSwgh56B64CzPCT8oWg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2020-02-06  4:47           ` EBkysko
       [not found]             ` <715e631d-41ba-44ad-b71f-918a917e57aa-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: EBkysko @ 2020-02-06  4:47 UTC (permalink / raw)
  To: pandoc-discuss


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

Sorry, I thought you were on the latest release (presently 2.9.1.1).

(Isn't Manjaro a rolling release distro? I'd thought they'd be up to date. 
Anyways, pandoc releases linux binaries also if Manjaro doesn't have the 
latest release.)

From pandoc 2.9 on, the "number" key-value was also put on the Div, rather 
than just on the following Header, so I used that one. See release note for 
pandoc 2.9.

If you still want to stay with your pandoc 2.8, here are the modified 
populate_section_numbers functions which use the "number" k-v on the Header 
(which will also work on 2.9.x) :


-- for pandoc 2.8+

function populate_section_numbers (doc)
  function populate(elements)
    for _,el in pairs(elements) do
      if el.t == "Div" and el.classes:includes("section") and el.content[1].t 
== "Header" and el.content[1].attributes.number ~= nil then
        section_numbers["#" .. el.identifier] = el.content[1].attributes.
number
        populate(el.content)
      end
    end
  end

  populate(make_sections(true, 1, doc.blocks))  -- number_sections true, 
base_level 1
end

-- or use this if you might be enclosing some sections within Divs

function populate_section_numbers_div (doc)
  function populate(elements)
    for _,el in pairs(elements) do
      if el.t == "Div" then
        if el.classes:includes("section") and el.content[1].t == "Header" 
and el.content[1].attributes.number ~= nil then
          section_numbers["#" .. el.identifier] = el.content[1].attributes.
number
        end
        populate(el.content)
      end
    end
  end

  populate(make_sections(true, 1, doc.blocks))  -- number_sections true, 
base_level 1
end

The above worked with a pandoc 2.8.1 binary I still had.

(sorry if there are misalignments in the code, cut & paste is wonky on 
GoogleGroups :( )


-- 
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/715e631d-41ba-44ad-b71f-918a917e57aa%40googlegroups.com.

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filter help
       [not found]             ` <715e631d-41ba-44ad-b71f-918a917e57aa-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2020-02-06  4:52               ` EBkysko
  2020-02-06  5:42               ` Alan Tyree
  1 sibling, 0 replies; 6+ messages in thread
From: EBkysko @ 2020-02-06  4:52 UTC (permalink / raw)
  To: pandoc-discuss


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

/Edit:

You might pretty things up a bit by creating the little function:

function is_secdiv_n(e)
  -- returns whether e is a proper section Div, with numbered Header
  return e.t == "Div" and e.classes:includes("section") and e.content[1].t 
== "Header" and e.content[1].attributes.number ~= nil
end

and use it like:

if is_secdiv_n(el) then
  -- do stuff here
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/f3cfc311-6d6d-464f-a797-723b2cf37078%40googlegroups.com.

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Filter help
       [not found]             ` <715e631d-41ba-44ad-b71f-918a917e57aa-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  2020-02-06  4:52               ` EBkysko
@ 2020-02-06  5:42               ` Alan Tyree
  1 sibling, 0 replies; 6+ messages in thread
From: Alan Tyree @ 2020-02-06  5:42 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

[-- Attachment #1: Type: text/plain, Size: 3287 bytes --]

Thank you so much. I think I am the one who should apologise. You had
clearly marked the filter as suitable for 2.9. Manjaro is a rolling release
-- I don't know why it is so far behind on the pandoc update.

Thanks again!
Alan

On Thu, 6 Feb 2020 at 15:47, EBkysko <ebkysko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:

> Sorry, I thought you were on the latest release (presently 2.9.1.1).
>
> (Isn't Manjaro a rolling release distro? I'd thought they'd be up to date.
> Anyways, pandoc releases linux binaries also if Manjaro doesn't have the
> latest release.)
>
> From pandoc 2.9 on, the "number" key-value was also put on the Div, rather
> than just on the following Header, so I used that one. See release note for
> pandoc 2.9.
>
> If you still want to stay with your pandoc 2.8, here are the modified
> populate_section_numbers functions which use the "number" k-v on the
> Header (which will also work on 2.9.x) :
>
>
> -- for pandoc 2.8+
>
> function populate_section_numbers (doc)
>   function populate(elements)
>     for _,el in pairs(elements) do
>       if el.t == "Div" and el.classes:includes("section") and el.content[1
> ].t == "Header" and el.content[1].attributes.number ~= nil then
>         section_numbers["#" .. el.identifier] = el.content[1].attributes.
> number
>         populate(el.content)
>       end
>     end
>   end
>
>   populate(make_sections(true, 1, doc.blocks))  -- number_sections true,
> base_level 1
> end
>
> -- or use this if you might be enclosing some sections within Divs
>
> function populate_section_numbers_div (doc)
>   function populate(elements)
>     for _,el in pairs(elements) do
>       if el.t == "Div" then
>         if el.classes:includes("section") and el.content[1].t == "Header"
> and el.content[1].attributes.number ~= nil then
>           section_numbers["#" .. el.identifier] = el.content[1].attributes
> .number
>         end
>         populate(el.content)
>       end
>     end
>   end
>
>   populate(make_sections(true, 1, doc.blocks))  -- number_sections true,
> base_level 1
> end
>
> The above worked with a pandoc 2.8.1 binary I still had.
>
> (sorry if there are misalignments in the code, cut & paste is wonky on
> GoogleGroups :( )
>
>
> --
> 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/715e631d-41ba-44ad-b71f-918a917e57aa%40googlegroups.com
> <https://groups.google.com/d/msgid/pandoc-discuss/715e631d-41ba-44ad-b71f-918a917e57aa%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Alan L Tyree                    http://www2.austlii.edu.au/~alan

-- 
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/CAGMsgMxEVWQ-DeSbOJB%3DySR%3D%2BXbQ0EaE3oBZgAUfCy27qPJd3A%40mail.gmail.com.

[-- Attachment #2: Type: text/html, Size: 15654 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-02-06  5:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-04  4:30 Filter help Alan Tyree
     [not found] ` <CAGMsgMwKsDspX=NfvqNNr1ZQMqLJOsYrGWHiMwBNwrZkNMiJXg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2020-02-05  3:37   ` EBkysko
     [not found]     ` <950405b0-1c8e-4842-8e1e-4d6e7fb6a909-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2020-02-06  0:24       ` Alan Tyree
     [not found]         ` <CAGMsgMxdyS5VkAib7bPEoH7MtQPHX13CSwgh56B64CzPCT8oWg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2020-02-06  4:47           ` EBkysko
     [not found]             ` <715e631d-41ba-44ad-b71f-918a917e57aa-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2020-02-06  4:52               ` EBkysko
2020-02-06  5:42               ` Alan Tyree

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).