Function to call a extern command as a filter
Terry Reedy
tjreedy at udel.edu
Wed Sep 25 15:30:33 EDT 2019
On 9/25/2019 7:27 AM, Rhodri James wrote:
> On 25/09/2019 12:04, Ulrich Goebel wrote:
>> Hello,
>>
>> what I want:
>>
>> I want a function which converts a markdown-formatted string to a
>> latex-formatted string
> [snip]
>> I have a extern tool pandoc which does exactly what I want, but it
>> works on files. This tool is able to work as a pipe, so it uses
>> the standard input and standard outpu.
I was not familiar with pandoc, but it seems to be a major m to n (where
n > m) markup translator. https://pandoc.org/index.html
https://github.com/jgm/pandoc (search 'pandoc github') says "Pandoc is a
Haskell library for converting from one markup format to another, and a
command-line tool that uses this library. "
If you were writing in Haskell or if pandoc were written in Python or if
pandoc were wrapped as a Python library, you could use it as an
importable library. The third is true, as it is for so many useful
libraries written in other languages.
https://pypi.org/project/pypandoc/ (https://github.com/bebraw/pypandod)
(search 'python pandoc') says it can use an existing pandoc installation
or pandoc included with its Windows and Mac wheels (for pip install).
https://github.com/applecrazy/pyandoc is another wrapping.
One can write custom filters in Python (and other languages).
https://pandoc.org/scripting-1.12.html
https://github.com/jgm/pandocfilters
https://pypi.org/project/pandocfilters/
>> What I could do:
>>
>> def markdown_to_latex (m : string)
>> write the string m to a file
>> call pandoc to work on that file
>> read the outputfile into the string l
>> return (l)
>>
>> What would be nice:
>>
>> I would like to avoid the extra steps writing an reading extern files.
>
> subprocess is your friend here. Something like:
Short of using pypandoc or pyandoc, this is probably the best thing.
> import subprocess
> def mardown_to_latex(markdown_string):
> latex = subprocess.run(cmd_string_for_pandoc,
> input=markdown_string,
> string=True,
> capture_output=True)
> return latex.output
>
> The above is completely untested and not in the least bit robust, but
> that's the area of the standard library you should be looking at.
>
--
Terry Jan Reedy
More information about the Python-list
mailing list