#!/usr/bin/python3 # Copyright 2023 Ted Clark # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License, version 2, as published by the Free # Software Foundation. # # This program is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. from sys import argv, exit, stderr def assert_head(): if not document_head: stderr.write('Missing document head\n') exit(1) def close_tags(): if state == 'QUOTE' or state == 'QUOTE2': print('') elif state == 'TD' or state == 'TD2': print('') elif state == 'TH' or state == 'TH2': print('') elif state == 'ULIST' or state == 'ULIST2': print('') def decode_string(string): decoded_str = '' skip = 0 for ii in range(0, len(string)): if skip: # Skip over HTML codes and tags skip -= 1 continue if string[ii] == '&': # Replace '&' with &, but don't change HTML codes such as   # to &nbsp;. codes = ('-', '✓', '&', 'ε', '>', '<', '—', ' ', '–') found = False for code in codes: if string[ii:ii + len(code)] == code: decoded_str += code skip = len(code) - 1 found = True break if found: continue decoded_str += '&' elif string[ii] == '<': # Replace '<' with <, but don't change HTML tags such as
# to <br>. tags = ('
', '', '', '', '', '', '', '', '', '', '', '

', '

', '
', '
', '', '', '', '', '', '', ) found = False for tag in tags: if string[ii:ii + len(tag)] == tag: decoded_str += tag skip = len(tag) - 1 found = True break if found: continue decoded_str += '<' elif string[ii] == '>': decoded_str += '>' else: decoded_str += string[ii] return decoded_str def get_state(line): global document_head, in_css, in_table, paragraph_num, state if line == '---': close_tags() state = 'NO_OP' return True elif line == '---body': if in_css: print('') in_css = False if not document_head: print('\n') document_head = True state = 'NO_OP' return True elif line == '---br': assert_head() # ---br does not close tags or change state, so table rows and tables # have to be explicitly closed. if in_table: print('\n') in_table = False state = 'NO_OP' print('
') return True elif line == '---c': close_tags() state = 'COMMENT' return True elif line == '---css': state = 'CSS' return True elif line == '---h1': assert_head() close_tags() state = 'H1' return True elif line == '---h2': assert_head() close_tags() state = 'H2' return True elif line == '---head': state = 'HEAD' return True elif line == '---l': assert_head() close_tags() state = 'LINK' return True elif line == '---pnum': paragraph_num = 1 elif line == '---q': assert_head() close_tags() state = 'QUOTE' return True elif line == '---raw': assert_head() close_tags() state = 'RAW' return True elif line == '---t': assert_head() close_tags() state = 'TEXT' return True elif line == '---td': assert_head() close_tags() state = 'TD' return True elif line == '---th': assert_head() close_tags() state = 'TH' return True elif line == '---ul': assert_head() close_tags() state = 'ULIST' return True return False def is_table_state(): if state == 'TD' or state == 'TD2' or state == 'TH' or state == 'TH2': return True return False def print_css(line): global in_css if not in_css: print('