public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* help with lua filter "Replacing placeholders with their metadata value"
@ 2023-01-12 16:59 Howard Borchew
       [not found] ` <8db3a838-8431-4b75-aac1-94bc29fac675n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: Howard Borchew @ 2023-01-12 16:59 UTC (permalink / raw)
  To: pandoc-discuss


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

Hello,

There is an example lua filter in pandoc.org called:
"Replacing placeholders with their metadata value"

This does exactly what I need but for one exception:
If I set a metadata value on the pandoc command line 
with --metadata=KEY[:VALUE]
then the placeholder is not replaced with anything and not the command line 
VALUE
using this example lua filter.

Is there an easy fix for this?

I am pasting the example from https://pandoc.org/lua-filters.html below.

Thanks,
Howard
----
local vars = {}

function get_vars (meta)
  for k, v in pairs(meta) do
    if pandoc.utils.type(v) == 'Inlines' then
      vars["%" .. k .. "%"] = {table.unpack(v)}
    end
  end
end

function replace (el)
  if vars[el.text] then
    return pandoc.Span(vars[el.text])
  else
    return el
  end
end

return {{Meta = get_vars}, {Str = replace}}

-- 
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/8db3a838-8431-4b75-aac1-94bc29fac675n%40googlegroups.com.

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

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

* Re: help with lua filter "Replacing placeholders with their metadata value"
       [not found] ` <8db3a838-8431-4b75-aac1-94bc29fac675n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2023-01-12 17:35   ` BPJ
       [not found]     ` <CADAJKhCJK59eix-A_-21UfnxnuDPi6r_jZV0RNSF01UuFzKcbA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: BPJ @ 2023-01-12 17:35 UTC (permalink / raw)
  To: pandoc-discuss

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

Den tors 12 jan. 2023 18:00Howard Borchew <hborchew-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev:

> Hello,
>
> There is an example lua filter in pandoc.org called:
> "Replacing placeholders with their metadata value"
>
> This does exactly what I need but for one exception:
> If I set a metadata value on the pandoc command line
> with --metadata=KEY[:VALUE]
> then the placeholder is not replaced with anything and not the command
> line VALUE
> using this example lua filter.
>

Probably because if you set the metadata value on the command line it is
just a raw string, not a list of inlines as the filter and pandoc.Span
expect.



> Is there an easy fix for this?
>

Set the variables in a metadata block in the document or in a metadata file.

<https://pandoc.org/MANUAL.html#option--metadata-file>



> I am pasting the example from https://pandoc.org/lua-filters.html below.
>
> Thanks,
> Howard
> ----
> local vars = {}
>
> function get_vars (meta)
>   for k, v in pairs(meta) do
>     if pandoc.utils.type(v) == 'Inlines' then
>       vars["%" .. k .. "%"] = {table.unpack(v)}
>     end
>   end
> end
>

It could be expanded to wrap a raw string value in a Str and an Inlines,
and to convert a Blocks value to an Inlines with
pandoc.utils.blocks_to_inlines.



> function replace (el)
>   if vars[el.text] then
>     return pandoc.Span(vars[el.text])
>   else
>     return el
>   end
> end
>
> return {{Meta = get_vars}, {Str = replace}}
>

My take is to use a Code or CodeBlock with class .var and the variable name
as text. More to type but more robust.

-- 
> 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/8db3a838-8431-4b75-aac1-94bc29fac675n%40googlegroups.com
> <https://groups.google.com/d/msgid/pandoc-discuss/8db3a838-8431-4b75-aac1-94bc29fac675n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CADAJKhCJK59eix-A_-21UfnxnuDPi6r_jZV0RNSF01UuFzKcbA%40mail.gmail.com.

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

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

* Re: help with lua filter "Replacing placeholders with their metadata value"
       [not found]     ` <CADAJKhCJK59eix-A_-21UfnxnuDPi6r_jZV0RNSF01UuFzKcbA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2023-01-13  0:13       ` Howard Borchew
       [not found]         ` <d1590311-ccb2-4e57-b518-be8c14c49ba8n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: Howard Borchew @ 2023-01-13  0:13 UTC (permalink / raw)
  To: pandoc-discuss


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



On Thursday, January 12, 2023 at 9:36:04 AM UTC-8 BPJ wrote:


Den tors 12 jan. 2023 18:00Howard Borchew <hbor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev:
Hello,

There is an example lua filter in pandoc.org called:
"Replacing placeholders with their metadata value"

This does exactly what I need but for one exception:
If I set a metadata value on the pandoc command line 
with --metadata=KEY[:VALUE]
then the placeholder is not replaced with anything and not the command line 
VALUE
using this example lua filter.

Probably because if you set the metadata value on the command line it is 
just a raw string, not a list of inlines as the filter and pandoc.Span 
expect.



Is there an easy fix for this?

Set the variables in a metadata block in the document or in a metadata file.

<https://pandoc.org/MANUAL.html#option--metadata-file>

The document is under revision control and I want to change placeholders 
from the command line at the time of running pandoc. 
 

I am pasting the example from https://pandoc.org/lua-filters.html below.

Thanks,
Howard
----
local vars = {}

function get_vars (meta)
  for k, v in pairs(meta) do
    if pandoc.utils.type(v) == 'Inlines' then
      vars["%" .. k .. "%"] = {table.unpack(v)}
    end
  end
end

It could be expanded to wrap a raw string value in a Str and an Inlines, 
and to convert a Blocks value to an Inlines with 
pandoc.utils.blocks_to_inlines.


I don't quite get your suggestion: I presume the list gets to get_vars 
through the argument meta and meta is iterated over in key(k)/value(v) 
pairs in the for-do loop.
Only when passing the value(v) type=="Inlines" check does a placeholder 
%key% and value(v) get added to the vars table.
While iterating over pairs(meta) would the for-do loop encounter the 
--metadata KEY[VALUE] items that were set by the command line switch?
Is it then simply a matter to pass that type check and add it to vars (type 
is raw string keyword?).



function replace (el)
  if vars[el.text] then
    return pandoc.Span(vars[el.text])
  else
    return el
  end
end

return {{Meta = get_vars}, {Str = replace}}

My take is to use a Code or CodeBlock with class .var and the variable name 
as text. More to type but more robust.


If I can get the command line KEY[VALUE] table items into vars above, I 
don't think this replace function would need to change.

Can you show some examples what you have in mind? Code or CodeBlock? 

-- 
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-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pandoc-discuss/8db3a838-8431-4b75-aac1-94bc29fac675n%40googlegroups.com 
<https://groups.google.com/d/msgid/pandoc-discuss/8db3a838-8431-4b75-aac1-94bc29fac675n%40googlegroups.com?utm_medium=email&utm_source=footer>
.

-- 
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/d1590311-ccb2-4e57-b518-be8c14c49ba8n%40googlegroups.com.

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

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

* Re: help with lua filter "Replacing placeholders with their metadata value"
       [not found]         ` <d1590311-ccb2-4e57-b518-be8c14c49ba8n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2023-01-13  9:11           ` Claudio Di Vita
  2023-01-14 21:35           ` BPJ
  1 sibling, 0 replies; 5+ messages in thread
From: Claudio Di Vita @ 2023-01-13  9:11 UTC (permalink / raw)
  To: pandoc-discuss


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

May I suggest to use this 
filter: https://github.com/cdivita/pandoc-curly-switch ?

The following syntax is supported for defining variables placeholders:

   - ${...}, the curly brackets syntax
   - !...!, the exclamation marks syntax

Variable can be referenced using an object-like notation, using any of the 
supported syntax. The exclamation marks syntax is suggested for LaTeX 
blocks (if you use them).

From the tests I've made it works fine also with metadata specified through 
the command line.

Cheers,

Claudio

Il giorno venerdì 13 gennaio 2023 alle 01:13:41 UTC+1 hbor...-Re5JQEeQqe8@public.gmane.orgm ha 
scritto:

> On Thursday, January 12, 2023 at 9:36:04 AM UTC-8 BPJ wrote:
>
>
> Den tors 12 jan. 2023 18:00Howard Borchew <hbor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev:
> Hello,
>
> There is an example lua filter in pandoc.org called:
> "Replacing placeholders with their metadata value"
>
> This does exactly what I need but for one exception:
> If I set a metadata value on the pandoc command line 
> with --metadata=KEY[:VALUE]
> then the placeholder is not replaced with anything and not the command 
> line VALUE
> using this example lua filter.
>
> Probably because if you set the metadata value on the command line it is 
> just a raw string, not a list of inlines as the filter and pandoc.Span 
> expect.
>
>
>
> Is there an easy fix for this?
>
> Set the variables in a metadata block in the document or in a metadata 
> file.
>
> <https://pandoc.org/MANUAL.html#option--metadata-file>
>
> The document is under revision control and I want to change placeholders 
> from the command line at the time of running pandoc. 
>  
>
> I am pasting the example from https://pandoc.org/lua-filters.html below.
>
> Thanks,
> Howard
> ----
> local vars = {}
>
> function get_vars (meta)
>   for k, v in pairs(meta) do
>     if pandoc.utils.type(v) == 'Inlines' then
>       vars["%" .. k .. "%"] = {table.unpack(v)}
>     end
>   end
> end
>
> It could be expanded to wrap a raw string value in a Str and an Inlines, 
> and to convert a Blocks value to an Inlines with 
> pandoc.utils.blocks_to_inlines.
>
>
> I don't quite get your suggestion: I presume the list gets to get_vars 
> through the argument meta and meta is iterated over in key(k)/value(v) 
> pairs in the for-do loop.
> Only when passing the value(v) type=="Inlines" check does a placeholder 
> %key% and value(v) get added to the vars table.
> While iterating over pairs(meta) would the for-do loop encounter the 
> --metadata KEY[VALUE] items that were set by the command line switch?
> Is it then simply a matter to pass that type check and add it to vars 
> (type is raw string keyword?).
>
>
>
> function replace (el)
>   if vars[el.text] then
>     return pandoc.Span(vars[el.text])
>   else
>     return el
>   end
> end
>
> return {{Meta = get_vars}, {Str = replace}}
>
> My take is to use a Code or CodeBlock with class .var and the variable 
> name as text. More to type but more robust.
>
>
> If I can get the command line KEY[VALUE] table items into vars above, I 
> don't think this replace function would need to change.
>
> Can you show some examples what you have in mind? Code or CodeBlock? 
>
> -- 
> 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-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/pandoc-discuss/8db3a838-8431-4b75-aac1-94bc29fac675n%40googlegroups.com 
> <https://groups.google.com/d/msgid/pandoc-discuss/8db3a838-8431-4b75-aac1-94bc29fac675n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/1d02199c-86ad-4730-9458-419babd9fab3n%40googlegroups.com.

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

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

* Re: help with lua filter "Replacing placeholders with their metadata value"
       [not found]         ` <d1590311-ccb2-4e57-b518-be8c14c49ba8n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  2023-01-13  9:11           ` Claudio Di Vita
@ 2023-01-14 21:35           ` BPJ
  1 sibling, 0 replies; 5+ messages in thread
From: BPJ @ 2023-01-14 21:35 UTC (permalink / raw)
  To: pandoc-discuss


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

I have written a README for my filter (both included in the attached zip
file) which should answer most questions about my approach.


Den fre 13 jan. 2023 01:14Howard Borchew <hborchew-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev:

>
>
> On Thursday, January 12, 2023 at 9:36:04 AM UTC-8 BPJ wrote:
>
>
> Den tors 12 jan. 2023 18:00Howard Borchew <hbor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev:
> Hello,
>
> There is an example lua filter in pandoc.org called:
> "Replacing placeholders with their metadata value"
>
> This does exactly what I need but for one exception:
> If I set a metadata value on the pandoc command line
> with --metadata=KEY[:VALUE]
> then the placeholder is not replaced with anything and not the command
> line VALUE
> using this example lua filter.
>
> Probably because if you set the metadata value on the command line it is
> just a raw string, not a list of inlines as the filter and pandoc.Span
> expect.
>
>
>
> Is there an easy fix for this?
>
> Set the variables in a metadata block in the document or in a metadata
> file.
>
> <https://pandoc.org/MANUAL.html#option--metadata-file>
>
> The document is under revision control and I want to change placeholders
> from the command line at the time of running pandoc.
>
>
> I am pasting the example from https://pandoc.org/lua-filters.html below.
>
> Thanks,
> Howard
> ----
> local vars = {}
>
> function get_vars (meta)
>   for k, v in pairs(meta) do
>     if pandoc.utils.type(v) == 'Inlines' then
>       vars["%" .. k .. "%"] = {table.unpack(v)}
>     end
>   end
> end
>
> It could be expanded to wrap a raw string value in a Str and an Inlines,
> and to convert a Blocks value to an Inlines with
> pandoc.utils.blocks_to_inlines.
>
>
> I don't quite get your suggestion: I presume the list gets to get_vars
> through the argument meta and meta is iterated over in key(k)/value(v)
> pairs in the for-do loop.
> Only when passing the value(v) type=="Inlines" check does a placeholder
> %key% and value(v) get added to the vars table.
> While iterating over pairs(meta) would the for-do loop encounter the
> --metadata KEY[VALUE] items that were set by the command line switch?
> Is it then simply a matter to pass that type check and add it to vars
> (type is raw string keyword?).
>
>
>
> function replace (el)
>   if vars[el.text] then
>     return pandoc.Span(vars[el.text])
>   else
>     return el
>   end
> end
>
> return {{Meta = get_vars}, {Str = replace}}
>
> My take is to use a Code or CodeBlock with class .var and the variable
> name as text. More to type but more robust.
>
>
> If I can get the command line KEY[VALUE] table items into vars above, I
> don't think this replace function would need to change.
>
> Can you show some examples what you have in mind? Code or CodeBlock?
>
> --
> 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-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/pandoc-discuss/8db3a838-8431-4b75-aac1-94bc29fac675n%40googlegroups.com
> <https://groups.google.com/d/msgid/pandoc-discuss/8db3a838-8431-4b75-aac1-94bc29fac675n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> --
> 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/d1590311-ccb2-4e57-b518-be8c14c49ba8n%40googlegroups.com
> <https://groups.google.com/d/msgid/pandoc-discuss/d1590311-ccb2-4e57-b518-be8c14c49ba8n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CADAJKhB50pgQNQ7yqeVCHC1JO9RovX-ARtHMTYKJM-H9XPfsNQ%40mail.gmail.com.

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

[-- Attachment #2: code2vars.zip --]
[-- Type: application/zip, Size: 12265 bytes --]

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

end of thread, other threads:[~2023-01-14 21:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-12 16:59 help with lua filter "Replacing placeholders with their metadata value" Howard Borchew
     [not found] ` <8db3a838-8431-4b75-aac1-94bc29fac675n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2023-01-12 17:35   ` BPJ
     [not found]     ` <CADAJKhCJK59eix-A_-21UfnxnuDPi6r_jZV0RNSF01UuFzKcbA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2023-01-13  0:13       ` Howard Borchew
     [not found]         ` <d1590311-ccb2-4e57-b518-be8c14c49ba8n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2023-01-13  9:11           ` Claudio Di Vita
2023-01-14 21:35           ` BPJ

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