user3508638
user3508638

Reputation: 325

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

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?

Upvotes: 7

Views: 9677

Answers (4)

nemszno
nemszno

Reputation: 41

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.

Upvotes: 2

tarleb
tarleb

Reputation: 22649

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 ...

Upvotes: 0

krlmlr
krlmlr

Reputation: 25484

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.

Upvotes: 2

Oliver Matthews
Oliver Matthews

Reputation: 7853

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

Upvotes: 2

Related Questions