|
| 1 | +import os, sys |
| 2 | +import re |
| 3 | +import cgi |
| 4 | + |
| 5 | +def parse(c): |
| 6 | + start_code = '----' |
| 7 | + end_code = '----' |
| 8 | + |
| 9 | + prompt_aside = '[ASIDE]' |
| 10 | + start_aside = '[' |
| 11 | + end_aside = ']' |
| 12 | + |
| 13 | + pState = 'out' |
| 14 | + codeState = 'out' |
| 15 | + asideState = 'out' |
| 16 | + listState = 'out' |
| 17 | + promptState = None |
| 18 | + |
| 19 | + lines = c.splitlines() |
| 20 | + |
| 21 | + sections = [] |
| 22 | + currentPara = '' |
| 23 | + currentCode = '' |
| 24 | + currentAside = '' |
| 25 | + currentList = '' |
| 26 | + |
| 27 | + for line in lines: |
| 28 | + sline = line.strip() |
| 29 | + if pState == 'in': |
| 30 | + if sline in ['', start_code, start_aside, prompt_aside]: |
| 31 | + pState = 'out' |
| 32 | + if currentPara: |
| 33 | + sections.append(('p', currentPara)) |
| 34 | + else: |
| 35 | + currentPara += ' ' + sline |
| 36 | + else: |
| 37 | + if (not (codeState == 'in' |
| 38 | + or asideState == 'in' |
| 39 | + or listState == 'in') |
| 40 | + and sline not in ['', start_code, start_aside, prompt_aside]): |
| 41 | + pState = 'in' |
| 42 | + currentPara = sline |
| 43 | + |
| 44 | + if codeState == 'in': |
| 45 | + if sline == end_code: |
| 46 | + codeState = 'out' |
| 47 | + if currentCode: |
| 48 | + sections.append(('code', currentCode)) |
| 49 | + else: |
| 50 | + currentCode += line + '\n' |
| 51 | + else: |
| 52 | + if sline == start_code: |
| 53 | + codeState = 'in' |
| 54 | + currentCode = '' |
| 55 | + |
| 56 | + if asideState == 'in': |
| 57 | + if sline == end_aside: |
| 58 | + asideState = 'out' |
| 59 | + if currentAside: |
| 60 | + aside = parse(currentAside) |
| 61 | + sections.append(('aside', aside)) |
| 62 | + else: |
| 63 | + currentAside += line + '\n' |
| 64 | + else: |
| 65 | + if sline == start_aside: |
| 66 | + asideState = 'in' |
| 67 | + currentAside = '' |
| 68 | + |
| 69 | + # clean up at the EOF |
| 70 | + if pState == 'in': |
| 71 | + if currentPara: |
| 72 | + sections.append(('p', currentPara)) |
| 73 | + |
| 74 | + if codeState == 'in': |
| 75 | + if currentCode: |
| 76 | + sections.append(('code', currentCode)) |
| 77 | + |
| 78 | + if asideState == 'in': |
| 79 | + if currentAside: |
| 80 | + aside = parse(currentAside) |
| 81 | + sections.append(('aside', aside)) |
| 82 | + |
| 83 | + return sections |
| 84 | + |
| 85 | +def render_dumb_html(sections): |
| 86 | + for sect in sections: |
| 87 | + if sect[0] == 'p': |
| 88 | + print '<p>' |
| 89 | + print sect[1] |
| 90 | + print '</p>' |
| 91 | + elif sect[0] == 'code': |
| 92 | + print '<pre>' |
| 93 | + print sect[1] |
| 94 | + print '</pre>' |
| 95 | + elif sect[0] == 'aside': |
| 96 | + print '<blockquote>' |
| 97 | + render_dumb_html(sect[1]) |
| 98 | + print '</blockquote>' |
| 99 | + |
| 100 | +def render_fodt(sections): |
| 101 | + fp = file('fodt_head.txt') |
| 102 | + c = fp.read() |
| 103 | + fp.close() |
| 104 | + write = sys.stdout.write |
| 105 | + write(c + '\n') |
| 106 | + def highlight_author_notes(text): |
| 107 | + s = '<text:span text:style-name="red_text">' |
| 108 | + e = '</text:span>' |
| 109 | + start_todo = text.find('[TODO') |
| 110 | + if start_todo == -1: |
| 111 | + return text |
| 112 | + end_todo = text.find(']', start_todo) |
| 113 | + text = (text[:start_todo] + s + |
| 114 | + text[start_todo:end_todo+1] |
| 115 | + + e + text[end_todo+1:]) |
| 116 | + return text |
| 117 | + |
| 118 | + def render_sections(sections, paraStyle="Body"): |
| 119 | + for sect in sections: |
| 120 | + if sect[0] == 'p': |
| 121 | + body = sect[1] |
| 122 | + body = cgi.escape(body) |
| 123 | + body = highlight_author_notes(body) |
| 124 | + write('<text:p text:style-name="%s">' % paraStyle) |
| 125 | + write(body) |
| 126 | + write('</text:p>\n') |
| 127 | + elif sect[0] == 'code': |
| 128 | + body = sect[1] |
| 129 | + body = cgi.escape(body) |
| 130 | + for line in body.splitlines(): |
| 131 | + result = re.split('\S', line, 1) |
| 132 | + if len(result) > 1: |
| 133 | + spaces = result[0] |
| 134 | + else: |
| 135 | + spaces = '' |
| 136 | + write('<text:p text:style-name="CodeB">') |
| 137 | + write('<text:s text:c="%d"/>' % len(spaces)) |
| 138 | + write(line[len(spaces):]) |
| 139 | + write('</text:p>\n') |
| 140 | + elif sect[0] == 'aside': |
| 141 | + #write('<text:p text:style-name="Note">') |
| 142 | + write('\n') |
| 143 | + render_sections(sect[1], 'Note') |
| 144 | + write('\n') |
| 145 | + #write('</text:p>\n') |
| 146 | + render_sections(sections) |
| 147 | + fp = file('fodt_tail.txt') |
| 148 | + c = fp.read() |
| 149 | + fp.close() |
| 150 | + write(c + '\n') |
| 151 | + |
| 152 | + |
| 153 | +def main(): |
| 154 | + chfile = sys.argv[1] |
| 155 | + chfile = file(chfile) |
| 156 | + chapter = chfile.read() |
| 157 | + chfile.close() |
| 158 | + |
| 159 | + sections = parse(chapter) |
| 160 | + #render_dumb_html(sections) |
| 161 | + render_fodt(sections) |
| 162 | + |
| 163 | +if __name__ == '__main__': |
| 164 | + main() |
0 commit comments