Easiest way to replace placeholders (variables) in markdown text?
Asked Answered
D

4

7

I have need for an open format to write stories in a generic way (with placeholder/variables). To make the stories case specific I would like to set a list of key/value pairs and apply them when publishing. I also want to separate styling from content so I can easily publish to web, pdf etc.

Example: @varname@ is a great place.

I chose Markdown to solve the content/styling separation but I do not know an easy way to parameterize certain parts of the text and have them filled in when I generate the html/pdf outputs with a tool like pandoc.

Any suggestions? Can this be done with markdown or is there a suitable markdown extension?

Dissemble answered 30/6, 2014 at 21:43 Comment(2)
Are you also working with a particular programming language? It seems to me that building templates that output Markdown is your best bet, but the details of that approach will depend heavily on your stack.Isbell
@Isbell : Not using a particular programming language. I am just looking for an easy to use/configurable key/value solution, e.g. pandoc-extension, to replace certain strings (keys) with corresponding values when publishing the markdown content to any output type (pdf,html,docx).Dissemble
L
2

Personally, I'd use something like sed, or even funnelweb, but you could do it with a pandoc filter, either in haskell or (probably easier) with the pandocfilters python library available here

Levitan answered 1/7, 2014 at 7:35 Comment(0)
S
2

For the web, you could use Jekyll templates with Liquid. Complex expressions surrounded by double braces {{}} are interpreted when converting to HTML. Example (from the link):

{{ site.time | date_to_string }}

07 Nov 2008

I think it's possible to define custom variables, too.

Starks answered 12/8, 2014 at 11:8 Comment(1)
best answer I have found to this question, thanksStridor
M
2

An easy way is to use CSS classes as definitions and HTML tags as placeholders. Just write anywhere in your document a custom style or add it to your renderer:

<style>
  area.varname1::after{content: "Berlin";}
  area.varname2::after{content: "Budapest";}
  ...
</style>

The style block won't be rendered but it will display the content of your variables anywhere you use the empty area HTML tag with the proper class in your document. For example if you write:

<area class="varname1"> is a great place.
You should also see <area class="varname2"> one day.
Trains go to <area class="varname2"> from <area class="varname1"> regularly.

Will be rendered as:

Berlin is a great place.
You should also see Budapest one day.
Trains go to Budapest from Berlin regularly.

This trick works if your markdown processor use HTML (a lot does). The area tag is great for this purpose as it is widely supported, rarely if ever used in markdown, short and can be empty. The ::after CSS selector inserts content as if it were originally written there, so all markdown formatting should work. For example: **<area class="varname1">** should become bold: Berlin

This simple solution is bit of a hack for like occasionally used templates, but probably works in any environment with almost all markdown setups out of the box.

Marelya answered 31/1, 2024 at 0:3 Comment(1)
If someone would like to use Markdown shortcut for inline code by using " ' " (apostrophe) around this (e.g. <area class=... >), then it doesn't work. What worked for me is <code><area class=... ></code>.Leanneleanor
P
0

Pandoc's Markdown allows to embed raw LaTeX and even replaces simple definitions, which can be leveraged using a Lua filter. E.g.;

\def\varname{Berlin}

\varname is a great place.

will work right away when producing PDF output.

Below is a Lua filter which makes this trick work for all output formats:

-- file: tex-macros.lua
function RawInline (r)
  -- Inline expanded TeX commands.
  if r.format == 'tex' and r.text:match '^[^\\]' then
    return pandoc.Str(r.text)
  end
end

Use it by passing it via the --lua-filter (or -L) command line option:

pandoc --lua-filter tex-macros.lua ...
Pavlish answered 20/4, 2021 at 7:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.