Sat, 07 Jun 2008

On Sat, Jun 7, 2008 at 11:42 AM, Aristotle Pagaltzis <pagaltzis at gmx.de> wrote:
> [Note to markdown-discuss readers: for context see
> <http://lists.canonical.org/pipermail/kragen-hacks/2008-June/000488.html>]
>
> * Kragen Javier Sitaker <kragen at pobox.com> [2008-06-07 09:40]:
>> Stylesheeting comes naturally. I just put a `<style>` element
>> at the top with a few lines inside of it to format nicely.
>
> Note that Markdown ends up wrapping `<link>` and `<style>`
> in `<p>` tags, arguably erroneously. Weirdly, it looks like
> Python-Markdown should avoid that mistake:

Well, first of all, he's using an old version of Python-Markdown. The
first line of his `render` function gives it away (due to the change
in 1.7 to all-unicode -- you generally don't pass unicode text to
str())

        body = str(markdown.Markdown(text))

Just use the wrapper (all lowercase in any version):

        body = markdown.markdown(text)

IIRC, there was a bugfix in 1.7 that also addressed the raw html
wrapped in <p> tags thing. So, upgrade to 1.7 and that problem should
go away.



-- 
----
Waylan Limberg
waylan at gmail.com
_______________________________________________
Markdown-Discuss mailing list
Markdown-Discuss at six.pairlist.net
http://six.pairlist.net/mailman/listinfo/markdown-discuss

[Note to markdown-discuss readers: for context see
<http://lists.canonical.org/pipermail/kragen-hacks/2008-June/000488.html>]

* Kragen Javier Sitaker <kragen at pobox.com> [2008-06-07 09:40]:
> Stylesheeting comes naturally. I just put a `<style>` element
> at the top with a few lines inside of it to format nicely.

Note that Markdown ends up wrapping `<link>` and `<style>`
in `<p>` tags, arguably erroneously. Weirdly, it looks like
Python-Markdown should avoid that mistake:

* <http://babelmark.bobtfish.net/?markdown=%3Cstyle%3Efoo+%7B%7D%3C%2Fstyle%3E>
* <http://babelmark.bobtfish.net/?markdown=%3Clink+%2F%3E>

But at the top of <http://canonical.org/~kragen/crywrap.html> the
problem shows up anyway.

Of course, neither tag has any business being in the HTML body;
they should both be in the head. Since you’re loading
BeautifulSoup anyway, you probably want to include that as fix-up
step in your postprocessing.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

Since working on this new project where we're using
<http://jottit.com/> for our to-do list, I've become enamored of
Markdown.  So I wrote this script to allow me to write documents
originally in Markdown and then generate HTML versions.

This works particularly well with Emacs `longlines-mode`.  I wrote the
first draft of my new company's "Acta Constitutiva" (i.e. charter and
bylaws) in it.  With CSS and with `M-x recompile` bound to the F5 key,
and `compile-command` set to `(cd ~/distributed-expertise;
~/devel/mkhtml.py bylaws; iceweasel bylaws.html)`, it was a pretty
reasonable word-processing experience, preferable to OpenOffice Write
for the following reasons:

- On my 384MB 700MHz laptop, OpenOffice is painfully slow; Emacs
  screams.
- My Emacs has an input method that handles Spanish reasonably;
  OpenOffice might, but I haven't been able to find it.
- I could edit in HTML and CSS in the cases where I wanted it, and
  pretend I was just editing a normal text file the rest of the time,
  with all of the normal Emacs amenities.  Except with
  word-processor-style word wrap instead of this M-q crap.
- Stylesheeting comes naturally.  I just put a `<style>` element at
  the top with a few lines inside of it to format nicely.
- I can see more of the document at a time in Emacs.

Like everything else posted to kragen-hacks without any notice to the
contrary, this program is in the public domain; I abandon any
copyright in it.

#!/usr/bin/python
"""Turn Markdown documents into HTML documents.

Depends on python-markdown and Beautiful Soup.

Markdown normally generates HTML document content; this generates HTML
documents instead.

"""
import markdown, BeautifulSoup, sys, os, os.path

def render(text):
    "Given Markdown input as a string, produce an HTML document as a string."
    body = str(markdown.Markdown(text))
    soup = BeautifulSoup.BeautifulSoup(body)

    headers = soup('h1')
    if len(headers) > 0:
        title = headers[0].renderContents()
    else:
        title = 'Lame document with no top-level header'

    return '''<html><head><title>%s</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <body>%s</body></html>''' % (title, body)

def process(infile):
    "Given a filename of Markdown input, create an HTML file as output."
    outfile = infile + '.html'

    if os.path.exists(outfile) and \
           os.stat(outfile).st_mtime > os.stat(infile).st_mtime:
        print "`%s` is newer than `%s`, skipping  " % (outfile, infile)
        return

    outfiletmp = outfile + '.tmp'
    fo = file(outfiletmp, 'w')
    fo.write(render(file(infile).read()))
    fo.close()

    os.rename(outfiletmp, outfile)  # atomic replace; won't work on Win32
    print "rendered `%s` to `%s`  " % (infile, outfile)

def main(args):
    filenames = args[1:]
    if filenames:
        for filename in filenames: process(filename)
        return 0
    else:
        print ("usage: `%s foo bar baz`; implicitly writes to `foo.html`, etc."
               % args[0])
        return 1

if __name__ == '__main__':
    sys.exit(main(sys.argv))