#!/usr/bin/env python
#!/usr/bin/python
# The idea is to convert from Chord Pro (chopro, chordpro, cho) format to HTML.
# This should be runnable from the command line and make files.
# This is pretty simple. So, garbage in will produce garbage out.
# BUG FIXED: when lines started with a chord (unwanted indent - blank td's) (see StormyWeather.chopro)
# TODO
# Do something more sophisticated with blank lines (not just blindly
).
# other directives? define?
# vertical bar (needed? [|] already works. Do we also need ['|]? Nah.)
# BUG: title directive must be first line in the file (so we can include it in the head title tag.
# 2011-07-12 - elb
import sys
import re
chord = re.compile(r'\[[^]]*\]')
lyric = re.compile(r'^[^[]*\[|\][^[]*\[|\][^[]*$')
# This could be better.
# Ideally each stanza should be a paragraph
# Each line (except the last) in each stanza should end with a
.
# That seems to require keeping a bunch of context.
# Or maybe we could switch it all around and use a state machine?
aChord = re.compile(r'\[([^]]*)\]')
inLyric = False
def lyricLine(theLine):
global inLyric
if empty.match(theLine):
if inLyric:
inLyric = False
return'
' + re.sub(aChord, r'\1', theLine.rstrip()) def lineVerbatim(theLine): return theLine.rstrip() empty = re.compile(r'\s*$') def xlyricLine(theLine): """Convert a line of lyrics into a two line table. The first row (line) should contain the chords (and vertical bars). The second row should contain the lyrics. If there are no chords, just one row with one cell. >>> lineAsTable("one [A]two [B#]three") '
| A | B# | |
| one | two | three |
| %s |
| %s | ' % i else: ourLine += '' ourTable += ' |
| %s | ' % i ourTable += '
%s
' % ('annotation', m.group(2)) elif 'key' == m.group(1): # This is awkward. Make it better somehow. if not gadgetsAdded: gadgetsAdded = addGadgets(m.group(2)) if gadgetsAdded: print 'in %s
' % ('annotation', m.group(2)) else: print 'in %s
' % ('annotation', m.group(2)) else: print 'in %s
' % ('annotation', m.group(2)) else: print '%s
' % (m.group(1), m.group(2)) elif (noargDirective.match(line)): m = noargDirective.match(line) if re.match(r'start_of_chorus|soc', m.group(1)): print '