Home » Questions » Computers [ Ask a new question ]

To convert markdown -files effectively to HTML -files

To convert markdown -files effectively to HTML -files

I have the following file structure

Asked by: Guest | Views: 222
Total answers/comments: 2
Guest [Entry]

"The existing bash-based answers will break on files with spaces in their names, and call unnecessary external commands to boot.

Assuming bash 4+ is available (it probably is, check with bash --version), you can set globstar for recursive globbing:

shopt -s globstar
for f in ./**/*.markdown; do markdown.py-2.6 ""$f"" > ""${f%.markdown}.html""; done

This can also be done with find; for absolute bulletproof-ness, you should use a nullbyte delimiter:

find . -name '*.markdown' -print0 | while read -d $'\0' f; do
markdown.py-2.6 ""$f"" > ""${f%.markdown}.html""
done

This particular problem could also be solved using find's -exec option, like so:

find . -name '*.markdown' -exec bash -c 'markdown.py-2.6 ""$0"" > ""${0%.markdown}.html"" '{}' \;

However, this is not as easy to extend to multi-line scripts."
Guest [Entry]

"You will probably find it easier to do this in bash (as in, once you understand the syntax it's only one or two lines), but for the record, here is how to do it in Python. You want to use two functions os.walk, and fnmatch.fnmatch to match the files you want in each directory. It looks like this:

#!/usr/bin/env python

import os, sys
from fnmatch import fnmatch

if len(sys.argv) != 2:
print ""Usage:"", sys.argv[0], ""<directory>""
sys.exit()

markdown = # <path to markdown.py>
directory = sys.argv[1]

for path, directory, files in os.walk(directory):
for file in files:
if fnmatch(file, ""*.html""):
html_file = ""%s/%s"" % (path, file)
markdown_file = html_file.replace("".html"", "".markdown"")
os.system(""python %s %s > %s"" % (markdown, markdown_file, html_file))

The main things to take away:

The os.walk function traverses a directory structure (using an generator). It returns three variables:

The current directory (path)
The list of directories found in the current directory (directories). You don't need this in this case.
The list of files found in the current directory (files). You do need this.

The fnmatch.fnmatch function takes a list of files and tells you if it matches a pattern. This is a shell ""glob"" pattern, and not a regular expression. You can use regular expressions here, but fnmatch is just easier for a simple case like this.

Note that you need to specify the path to the markdown script. Even better would be to not use os.system but instead to import markdown the module and call it's primary function, but this generalizes to non-Python programs. (Plus, I don't know exactly what that function would be :)."