asmaier
asmaier

Reputation: 11776

Splitting a document with custom pandoc lua writer

I would like to split a LaTeX document into several pandoc markdown documents. The split should happen at each \section{section_name} and the section name should be used as output name for the corresponding markdown file. So a latex file like

\section{name1}
some text
\section{name2}
more text

should be split into two markdown documents with names name1.md and name2.md. Is this possible with a pandoc custom writer in Lua at all? I couldn't find any useful example in the documentation https://pandoc.org/custom-writers.html#new-style on how to approach this or if it is even possible with custom writers to generate multiple output files from a single input file.

Upvotes: 1

Views: 221

Answers (1)

Leathong
Leathong

Reputation: 1

Custom writer is not for that.

You can use Python to do this

import re
import os

input = """
\section{name1}
some text
\section{name2}
more text
"""

section_re = re.compile(r'\\section\{([^\{\}]+)\}\n(([\s\S](?!\\section\{))+[\s\S])',re.M)

matchs = section_re.findall(input)

dir_path = os.path.expanduser("~/Documents")

for match in matchs:
    file_path = os.path.join(dir_path, match[0] + '.md')
    file = open(file_path, "w+")
    file.write(match[1])
    file.close()

Upvotes: 0

Related Questions