14

First, let me say - I love markdown. Truly love it. It's simple, it's elegant, it's sexy, it's everything I wish for in a markup language. If I could, I'd propose to it :)

So far I've been using it in a very nice and simple way, Vim + python-markdown = fast preview in my browser of choice.

But, it has one drawback ... the css sheet is hardcoded somewhere inside the plugin, and I can't change it. Note: I know zero python, or something very close to it.

Is there a markdown to -various formats- plugin that lets you specify a css page to use, so that I could have several and create several versions of the same document using the one I wish at that time?

It would go something like

markdown  my-document-in.markdown  css-sheet.css  cool-looking-document.html
5
  • 2
    +1 for loving markdown. me too. Commented Feb 16, 2012 at 22:55
  • @thephpdeveloper - No, seriously, it's one really beautiful thing. Simple yet so useful. If it had math support (for equations) I'd probably propose to it. Commented Feb 16, 2012 at 23:00
  • @Jonas - Looking at it now; I don't see anything in the docs regarding entering a chosen css sheet. Could you provide a hint or two? Commented Feb 16, 2012 at 23:07
  • @applicative - Why did you delete the answer? I checked out pandoc, and indeed, it is a wonderful project. Commented Feb 20, 2012 at 14:41
  • I'll add this as a comment rather than an answer because you'll probably hate me for suggesting it! The cross-platform editor "Sublime Text 2" has this capability via a plugin called "Markdown Preview". The CSS is in the plugins folder and is easily edited. Interestingly, ST2 is written in Python. Commented Jun 22, 2012 at 21:35

1 Answer 1

7

Using https://github.com/trentm/python-markdown2/ (specifically https://raw.github.com/trentm/python-markdown2/master/lib/markdown2.py), I wrote a small script which when called as generator.py input.markdown styles.css pretty.html (assuming you saved it as generator.py) uses the python-markdown2 library to convert the markdown to HTML, embeds the css file in the top and writes it to pretty.html.

import markdown2
import os, sys


output = """<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <style type="text/css">
"""

cssin = open(sys.argv[2])
output += cssin.read()

output += """
    </style>
</head>

<body>
"""
mkin = open(sys.argv[1])
output += markdown2.markdown(mkin.read())

output += """</body>

</html>
"""

outfile = open(sys.argv[3])
outfile.write(output)
outfile.close()`

Copy the linked file from github and the code above into a folder together and it should run fine. I've tested it locally and it works. Hope it can help you too.

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

1 Comment

Note that since strings are immutable in python, using += is a horrible thing to do, performance-wise. Since you're opening outfile anyway, better just write each piece separately.

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.