public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* filter to inject raw TeX command based on header attributes
@ 2021-12-23 15:14 bapt auguie
       [not found] ` <CAGCXF28DUfNvU8cVDjydiqg+TUwzOhdrWL5w5xc8Ua-MXA6BBA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: bapt auguie @ 2021-12-23 15:14 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

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

Hi,

I'd like to make use of pandoc's header attributes (
https://pandoc.org/MANUAL.html#extension-header_attributes) to inject a
custom LaTeX \renewcommand{} definition in specific sections. My goal is to
have a minimalist markdown file for my CV (no tables or raw LaTeX, just
simple lists and #headers), and have a Lua filter identify which sections
have specific formatting needs (e.g wrap inside a 3-column table), which
will be taken care of by LaTeX.

Consider this input markdown,


> # qualifications {#id1 .CV content=list}
>
> ## 2011 | can type, delete, copy and paste
> ## 2021 | can *format*, sometimes
>
> # work in progress {#id2 .CV content=freetext}
>
> ## Beside lists, I am hoping to learn to format free text too, *someday*.
>


I would like to walk the AST, and immediately after headers with a specific
attribute, inject a \renewcommand{} based on the header's attributes, such
as (dummy example),

\renewcommand\subsection[1]{\textbf{#1}}

for sections with content=list and

\renewcommand\subsection[1]{\textit{#1}}

with content=freetext.

This is obviously a contrived example, the actual command I'm using is
actually to split a sub-header and format it as a table (dates | job
position | location) or various other tasks (e.g. changing emphasis colours
for different sections etc.).

I'm not familiar enough with the pandoc AST or lua to figure this out; I
looked at a related example ( * noexport-subtrees.lua *
<https://gist.github.com/tarleb/a0f41adfa7b0e5a9be441e945f843299#file-noexport-subtrees-lua>)
but here the situation is a bit different, as I need to create an
additional element (raw tex string) and append it to the original header.

Many thanks,

baptiste

-- 
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/CAGCXF28DUfNvU8cVDjydiqg%2BTUwzOhdrWL5w5xc8Ua-MXA6BBA%40mail.gmail.com.

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

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

* Re: filter to inject raw TeX command based on header attributes
       [not found] ` <CAGCXF28DUfNvU8cVDjydiqg+TUwzOhdrWL5w5xc8Ua-MXA6BBA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2021-12-29 19:41   ` John MacFarlane
       [not found]     ` <m2a6gjyza0.fsf-jF64zX8BO0+FqBokazbCQ6OPv3vYUT2dxr7GGTnW70NeoWH0uzbU5w@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: John MacFarlane @ 2021-12-29 19:41 UTC (permalink / raw)
  To: bapt auguie, pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

bapt auguie <auguieba-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:

> I would like to walk the AST, and immediately after headers with a specific
> attribute, inject a \renewcommand{} based on the header's attributes, such
> as (dummy example),
>
> \renewcommand\subsection[1]{\textbf{#1}}

This is pretty easy.  You would have a function

function Header(el)
  local attr = el.attributes
  -- you can do attr['foo'] to get the value of the 'foo' attribute
  -- now create a string with the macro:
  local macro = ...
  return { el, pandoc.RawBlock("latex", macro) }
end

You can fill in the blanks.


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

* Re: filter to inject raw TeX command based on header attributes
       [not found]     ` <m2a6gjyza0.fsf-jF64zX8BO0+FqBokazbCQ6OPv3vYUT2dxr7GGTnW70NeoWH0uzbU5w@public.gmane.org>
@ 2021-12-29 20:45       ` bapt auguie
       [not found]         ` <CAGCXF2_r6G_sMQWmxyXKA5i29gRFLtOG5Z-WowKXkHASThsGqQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: bapt auguie @ 2021-12-29 20:45 UTC (permalink / raw)
  To: John MacFarlane; +Cc: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

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

Excellent, I managed to adapt it and it works a charm!

Thanks,

b.

On Wed, 29 Dec 2021 at 20:42, John MacFarlane <jgm-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> wrote:

> bapt auguie <auguieba-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:
>
> > I would like to walk the AST, and immediately after headers with a
> specific
> > attribute, inject a \renewcommand{} based on the header's attributes,
> such
> > as (dummy example),
> >
> > \renewcommand\subsection[1]{\textbf{#1}}
>
> This is pretty easy.  You would have a function
>
> function Header(el)
>   local attr = el.attributes
>   -- you can do attr['foo'] to get the value of the 'foo' attribute
>   -- now create a string with the macro:
>   local macro = ...
>   return { el, pandoc.RawBlock("latex", macro) }
> end
>
> You can fill in the blanks.
>
>

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

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

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

* Re: filter to inject raw TeX command based on header attributes
       [not found]         ` <CAGCXF2_r6G_sMQWmxyXKA5i29gRFLtOG5Z-WowKXkHASThsGqQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2021-12-31 11:52           ` BPJ
  0 siblings, 0 replies; 4+ messages in thread
From: BPJ @ 2021-12-31 11:52 UTC (permalink / raw)
  To: pandoc-discuss

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

I think I mentioned this recently but a useful trick when using a filter to
convert most code spans/blocks into raws or something else is to use a
dummy class `.code` on those which don't have any other class(es) and
shouldn't be converted:

``````lua
local function no_code_class (it) return it ~= 'code' end

function Code (elem)
  -- Skip if it has any classes
  if 0 < #elem.classes then
    -- Remove the 'code' class
    elem.classes = elem.classes:filter(no_code_class)
    return elem
  else
    -- Do whatever you want with a "bare" Code e.g.
    return pandoc.RawInline(SOME_FORMAT, elem.text)
  end
end
``````


Den ons 29 dec. 2021 21:46bapt auguie <auguieba-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev:

> Excellent, I managed to adapt it and it works a charm!
>
> Thanks,
>
> b.
>
> On Wed, 29 Dec 2021 at 20:42, John MacFarlane <jgm-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> wrote:
>
>> bapt auguie <auguieba-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:
>>
>> > I would like to walk the AST, and immediately after headers with a
>> specific
>> > attribute, inject a \renewcommand{} based on the header's attributes,
>> such
>> > as (dummy example),
>> >
>> > \renewcommand\subsection[1]{\textbf{#1}}
>>
>> This is pretty easy.  You would have a function
>>
>> function Header(el)
>>   local attr = el.attributes
>>   -- you can do attr['foo'] to get the value of the 'foo' attribute
>>   -- now create a string with the macro:
>>   local macro = ...
>>   return { el, pandoc.RawBlock("latex", macro) }
>> end
>>
>> You can fill in the blanks.
>>
>> --
> 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/CAGCXF2_r6G_sMQWmxyXKA5i29gRFLtOG5Z-WowKXkHASThsGqQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/pandoc-discuss/CAGCXF2_r6G_sMQWmxyXKA5i29gRFLtOG5Z-WowKXkHASThsGqQ%40mail.gmail.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/CADAJKhDE3NyVp%3DCDNczGtUkOd2mPjw6zdxRRdYd5xzgOKmLXNA%40mail.gmail.com.

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

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

end of thread, other threads:[~2021-12-31 11:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-23 15:14 filter to inject raw TeX command based on header attributes bapt auguie
     [not found] ` <CAGCXF28DUfNvU8cVDjydiqg+TUwzOhdrWL5w5xc8Ua-MXA6BBA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2021-12-29 19:41   ` John MacFarlane
     [not found]     ` <m2a6gjyza0.fsf-jF64zX8BO0+FqBokazbCQ6OPv3vYUT2dxr7GGTnW70NeoWH0uzbU5w@public.gmane.org>
2021-12-29 20:45       ` bapt auguie
     [not found]         ` <CAGCXF2_r6G_sMQWmxyXKA5i29gRFLtOG5Z-WowKXkHASThsGqQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2021-12-31 11:52           ` 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).