0

Is there any way to make a bit of text bold using the 'pandocfilters' package (https://github.com/jgm/pandocfilters/tree/master/examples) in pandoc?

As a minimal working example, suppose I have a markdown file ('foo.md'):

foobar.

I want to write 'filter.py' to be something like

from pandocfilters import toJSONFilter, Str,Emph

def boldify(key, val, fmt, meta):
    if key == 'Str' and "foo" in val:
        # this is the part I can't don't know how to do
        # I would like to make the value be bold
        return [Emph(val)]

if __name__ == '__main__':
    toJSONFilter(boldify)

So we run the whole thing like

pandoc 'foo.md' --filter='filter.py' -o 'foo.docx'

Using this, I get the following error:

pandoc: Error in $.blocks[0].c[0].c: expected [a], encountered String
CallStack (from HasCallStack):
error, called at pandoc.hs:144:42 in main:Main

Any help would be appreciated.

heisenBug
  • 865
  • 9
  • 19

1 Answers1

0

I found an answer, and I will post what I found (and how I discovered it) in hopes of helping someone else.

What I found

The Strong (or bolded) element requires a list of Str elements as its input for this to work

We should change filter.py to return

[Strong([Str(val)])]

instead of

[Strong(val)]

or

[Strong(Str(val))]    

How I found the answer

If we change "foo.md" to:

**(foo)** bar

And get the abstract syntax tree via:

pandoc -t json -s foo.md

we get

{"blocks":[{"t":"Para","c":[{"t":"Strong","c":[{"t":"Str","c":"(foo)"}]},{"t":"Space"},{"t":"Str","c":"bar"}]}],"pandoc-api-version":[1,17,0,5],"meta":{}}[1]+ 

In particular, the bit:

{"t":"Strong","c":[{"t":"Str","c":"(foo)"}]}

tells us the formatting we need to get a working AST.

Community
  • 1
  • 1
heisenBug
  • 865
  • 9
  • 19