#!/usr/bin/php \n"; break; case 'TD': case 'TD2': case 'TH': case 'TH2': print "\n"; break; case 'ULIST': case 'ULIST2': print "\n"; break; } } function decode_string(string $string) : string { $decoded_str = ''; $skip = 0; for ($ii = 0; $ii < strlen($string); $ii++) { if ($skip) { # Skip over HTML codes and tags $skip--; continue; } if ($string[$ii] == '&') { # Replace '&' with &, but don't change HTML codes such as   # to &nbsp;. $codes = array('-', '✓', '&', 'ε', '>', '<', '—', ' ', '–'); $found = False; foreach ($codes as $code) { if (substr($string, $ii, strlen($code)) == $code) { $decoded_str .= $code; $skip = strlen($code) - 1; $found = True; break; } } if ($found) { continue; } $decoded_str .= '&'; } else if ($string[$ii] == '<') { # Replace '<' with <, but don't change HTML tags such as
# to <br>. $tags = array('
', '', '', '', '', '', '', '', '', '', '', '

', '

', '
', '
', '', '', '', '', '', '', ); $found = False; foreach ($tags as $tag) { if (substr($string, $ii, strlen($tag)) == $tag) { $decoded_str .= $tag; $skip = strlen($tag) - 1; $found = True; break; } } if ($found) { continue; } $decoded_str .= '<'; } else if ($string[$ii] == '>') { $decoded_str .= '>'; } else { $decoded_str .= $string[$ii]; } } return $decoded_str; } function get_state(string $line) : bool { global $document_head, $in_css, $in_table, $paragraph_num, $state; switch ($line) { case '---': close_tags(); $state = 'NO_OP'; return True; case '---body': if ($in_css) { print "\n"; $in_css = False; } if (!$document_head) { print "\n\n"; $document_head = True; } $state = 'NO_OP'; return True; case '---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\n"; $in_table = False; $state = 'NO_OP'; } print "
\n"; return True; case '---c': close_tags(); $state = 'COMMENT'; return True; case '---css': $state = 'CSS'; return True; case '---h1': assert_head(); close_tags(); $state = 'H1'; return True; case '---h2': assert_head(); close_tags(); $state = 'H2'; return True; case '---head': $state = 'HEAD'; return True; case '---l': assert_head(); close_tags(); $state = 'LINK'; return True; case '---pnum': $paragraph_num = 1; return True; case '---q': assert_head(); close_tags(); $state = 'QUOTE'; return True; case '---raw': assert_head(); close_tags(); $state = 'RAW'; return True; case '---t': assert_head(); close_tags(); $state = 'TEXT'; return True; case '---td': assert_head(); close_tags(); $state = 'TD'; return True; case '---th': assert_head(); close_tags(); $state = 'TH'; return True; case '---ul': assert_head(); close_tags(); $state = 'ULIST'; return True; } return False; } function is_table_state() : bool { global $state; if ($state == 'TD' || $state == 'TD2' || $state == 'TH' || $state == 'TH2') { return True; } return False; } function print_css(string $line) { global $in_css; if (!$in_css) { print "