0

I've currently been using pandoc to convert some markdown into html, and wanted to invent my own syntax for certain repeated areas, I was hoping to generate a block of code that takes "variable like" inputs, but I'm not sure where I would start.

For example, I wanted to convert this:

~ question-multiple
    question:
        Which animal is supermarket milk usually obtained from?
    options:
        + Dog
        + Cat
        + Cow
        + Sheep

Into the following block of html:

<div class="question multiple">
<div>Which animal is supermarket milk usually obtained from?</div>
<div>
    <li>Dog</li>
    <li>Cat</li>
    <li>Cow</li>
    <li>Sheep</li>
</div>

I know I can achieve this using a text converter, but I wanted to do it in directly inside of pandoc. I figured I'd just use a template, but I'm not sure how I'd pass anything into the template.

I also looked into filters, but they seem to operate on single node (or word as far as I understand) at a time. Does anybody know how I would be able to achieve this?

5
  • Any reason why you can't use a definition list for this? Commented Apr 11, 2016 at 15:09
  • I don't know how I'd turn that into the html I was after. Is there an easy way to do that? Commented Apr 11, 2016 at 15:18
  • I was thinking the generated definition list could be used as-is. Or do you "need" your proposed output? Commented Apr 11, 2016 at 15:22
  • BTW, you proposed output is not valid. You need a <ul> or <ol> wrapping your <li> tags. Commented Apr 11, 2016 at 15:24
  • Yep, the proposed output is basically what I should be using. (sorry yes, I forgot to wrap the list in a <ul> tag :P My mistake). I'm basically using pandoc to make writing easier here. Commented Apr 11, 2016 at 15:48

1 Answer 1

1

Seems to me that you shouldn't need to "invent" your own syntax. Especially as Pandoc already supports definition lists. You could represent your question and answer like this:

Which animal is supermarket milk usually obtained from?
: Dog
: Cat
: Cow
: Sheep

Which would result in the following output:

<dl>
  <dt>Which animal is supermarket milk usually obtained from?</dt>
  <dd>Dog</dd>
  <dd>Cat</dd>
  <dd>Cow</dd>
  <dd>Sheep</dd>
</dl>

Note about semantics: While they are called "definition lists" in Markdown (as well as in HTML4 and XHTML1), the HTML5 spec renamed them to "description lists" and expanded their intended uses to include (emphasis added):

... terms and definitions, metadata topics and values, questions and answers, or any other groups of name-value data.

Therefore, this is already an intended use case for "description lists" and will display in a sensible way in most browsers without any special styling.

If you need some additional hooks, you could include the list in an HTML block. As the Pandoc User Guide states:

[B]y default, pandoc interprets material between HTML block tags as Markdown.

This departure from standard Markdown should make it easier to mix Markdown with HTML block elements. For example, one can surround a block of Markdown text with tags without preventing it from being interpreted as Markdown.

Therefore, your Markdown might look like this:

<div class="question multiple">

Which animal is supermarket milk usually obtained from?
: Dog
: Cat
: Cow
: Sheep

</div>

As an aside, it may be helpful to recall that the creator of Markdown gives the following explanation for supporting raw HTML in Markdown:

Markdown is not a replacement for HTML, or even close to it. Its syntax is very small, corresponding only to a very small subset of HTML tags. The idea is not to create a syntax that makes it easier to insert HTML tags. In my opinion, HTML tags are already easy to insert. The idea for Markdown is to make it easy to read, write, and edit prose. HTML is a publishing format; Markdown is a writing format. Thus, Markdown’s formatting syntax only addresses issues that can be conveyed in plain text.

For any markup that is not covered by Markdown’s syntax, you simply use HTML itself.

If you need to create your own syntax, then perhaps your doing it wrong, or you shouldn't be using Markdown.

Sign up to request clarification or add additional context in comments.

2 Comments

This is awesome, and thanks for pointing me at that, this is definitely a good solution :) The problem here is more general though. Basically, I'm going to have an article with a range of different items. For example, I'm going to have hidden sections that I will add interaction to within the article; and I'd like to use a convenient syntax that is very explicit. For example, I'd specifically want the class applied to the question, otherwise; I'd have to manually add it later, which is something I'm trying to avoid. Effectively, I want the syntax to be an explicit "template" for the html.
I added some info about using raw HTML. Does that meet your needs?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.