Calaf
Calaf

Reputation: 10837

How to produce a complete HTML document using markdown?

We can process the file code.md containing

Here is our first program.

```python
s = "Hello, World!"
print s
```

using

markdown2-2.7 -x fenced-code-blocks code.md > code.html

to produce the HTML

<p>Here is our first program.</p>

<div class="codehilite"><pre><code><span class="n">s</span> <span class="o">=</span> <span class="s">&quot;Hello, World!&quot;</span>
<span class="k">print</span> <span class="n">s</span>
</code></pre></div>

But then it's necessary to add html, head, and body tags as a postprocess, in addition to linking to a css stylesheet for syntax highlighting.

Is markdown2 really meant for this kind of workflow, or can the postprocess be incorporated a bit more seamlessly (without, for example, wrapping with an additional script)?

I am using markdown2 and pygments installed on OS X through:

sudo port install py27-markdown2
sudo port install py27-pygments

Upvotes: 0

Views: 285

Answers (1)

Waylan
Waylan

Reputation: 42557

Most implementations of Markdown (including the original Perl implementation), only output an HTML fragment which you are then expected to pass to a template or otherwise wrap in an HTML <body>. If you search around, there are a few implementations that may include a feature to optionally output an entire document, but they are rare. Pandoc is probably the most prominent one, but that is also a lot more than a Markdown parser.

If you want to continue using the implementation to already have from the command line, the following should work (where head.html and foot.html contain everything before and after the Markdown fragment respectively):

cat head.html > code.html
markdown2-2.7 -x fenced-code-blocks code.md >> code.html 
cat foot.html >> code.html

Note that the >> operator appends to the end of a file rather than overwriting it.

Of course, this will not give you a <title> in your <head> and that, I expect, is why Markdown does not officially offer this as a feature. It is also why a templating system is generally preferred.

So to answer your question, yes, with a few rare exceptions, you are expected to write a script to wrap Markdown to get full HTML documents.

Upvotes: 1

Related Questions