2) { year = yy + 1900; year=year.toString();}
else {
if (yy > 70) { year = yy + 1900; year=year.toString(); }
else { year = yy + 2000; year=year.toString(); }
}
}
thedate=year+"-"+ mmm+"-"+ddd; return (thedate);
}
*/
ini_set('display_errors', '0');
date_default_timezone_set('America/Los_Angeles');
// == PARSE LINE
// A function to parse lines for inline phrasing like _emphasis_, *strong*, and `code`.
function parse_line($line_in) {
$line_out = "";
$pre = false;
$emp = false;
$str = false;
for ($i = 0, $length = mb_strlen($line_in); $i < $length; $i++) {
$char = mb_substr($line_in, $i, 1);
if($char == "`") {
$pre = !$pre;
if($pre) {
$line_out .= " Contents of this page: $link_label $link_label $link_label ".parse_line(htmlentities($line))." ".parse_line(htmlentities($line))."";
continue;
} else {
$line_out .= "";
continue;
}
} elseif ($char == "_" && !$pre) {
$emp = !$emp;
if($emp) {
$line_out .= "";
continue;
} else {
$line_out .= "";
continue;
}
} else if($char == "*" && !$pre) {
$str = !$str;
if($str) {
$line_out .= "";
continue;
} else {
$line_out .= "";
continue;
}
}
$line_out .= $char;
}
// clean up mismatched inline markup so it doesn't pollute the rest of the page
if($pre) $line_out .= "";
if($emp) $line_out .= "";
if($str) $line_out .= "";
return $line_out;
} // END parse_line
// == INITIALIZE VARIABLES
$host = $_SERVER['HTTP_HOST'];
if ($host == "localhost" or $host == "127.0.0.1") {
// call as
\n";
$img_extensions = array("png", "apng", "avif", "bmp", "ico", "tif", "tiff", "jpg", "jpeg", "gif", "svg", "webp");
$aud_extensions = array("mp3", "wav", "ogg", "m4a","opus","flac");
$vid_extensions = array("mp4", "ogg", "webm");
// == MAIN LOOP
while(!feof($file)) {
$line_no += 1;
$line_untrimmed = fgets($file);
$line = trim($line_untrimmed);
$line_len = strlen($line);
// == POLYGLOT gemtext/pdf
// If this is gemtext/pdf polyglot .gmi skip the first four lines; this only works with files generated by gemdoc from https://github.com/seifferth/gemdoc
if($line_no == 1) {
if ($line == "%PDF-1.7") {
$is_pdf = true;
$body .= "\n";
continue;
} else { // ToDo: rework this: what if the first line of the file begins ">". "=>", "*", or "```"?
$lntype = $line[0] == "#" ? "h1" : "p"; // index.gmi uses "👴 jdcard" as the level header, in other pages it is a plain line
$body .= "\n";
if ($lntype == "h1") {$title = htmlentities(substr($line, 2, $line_len-2)); }
$body .= " <$lntype>".htmlentities($lntype == "h1" ? substr($line, 2, $line_len-2) : $line)."$lntype>\n"; // index.gmi
continue;
}
}
// skip past the pdf data that we don't want to see
if($is_pdf == true and $line_no <5) {
continue;
}
// If this is a gemtext/pdf polyglot file, create a link to view it as pdf, then stop processing the file at the end of the gemtext portion
if($is_pdf == true and $line == "endstream") {
$pdf_file = str_replace(".gmi", ".pdf", $fname); // create name for pdf symlink
$pdf_file = substr($pdf_file, 1); // trim the leading "/" path character
$fname = preg_replace("/\/.*\//", "", $fname); // strip path, leave file-name
$fname = preg_replace("/\/?/", "", $fname); // previous line sometimes left an inital "/"
symlink($fname, $pdf_file); // create symlink to this file with a .pdf extension
$pdf_file = preg_replace("/^.*\//", "", $pdf_file); // strip path, leave file-name
$body .= "
\n"; // add a link in this document to the pdf file
// $body .= " "; // DEBUG
break;
}
// == PREFORMATTED
if($line_len >= 3 && substr($line, 0, 3) == "```") {
if($blockquote_open && $line[0] != ">") {$blockquote_open = false;}
if($list_open) {$body .= " \n"; $list_open = false;}
$preformatted = !$preformatted;
if($preformatted) {
$body .= "
\n ";
} else {
$body .= "\n";
}
continue;
}
if($preformatted) {
$body .= htmlentities($line_untrimmed);
continue;
}
// == THEMATIC BREAK - horizontal rule; ⁂ "Asterism" character, see https://en.wikipedia.org/wiki/Dinkus
if(substr($line, 0, 3) == "---" || $line == "⁂" ) { // I don't use "---" and it accidentally creates
elements inside
// if($line == "⁂" ) {
$body .= "
\n";
continue;
}
// == LINKS
if(strpos($line, "=>") === 0) {
$line = ltrim($line, "=> \t");
if(strlen($line) == 0) {
continue;
}
$first_space = strpos($line, " ");
$first_tab = strpos($line, "\t");
$link_target = "";
$link_label = "";
if($first_space === false && $first_tab === false) {
$link_target = $line;
$link_label = $line;
} else {
if($first_space === false) $first_space = 999999;
if($first_tab === false) $first_tab = 999999;
$parts = [];
if($first_space < $first_tab) {
$parts = explode(" ", $line, 2);
} else {
$parts = explode("\t", $line, 2);
}
$link_target = $parts[0];
$link_label = parse_line(htmlentities($parts[1]));
}
$extension = substr(strrchr($link_target, '.'), 1);
// == INLINE DISPLAY of linked images, audio, and video content ====================================================================================
if(in_array(strtolower($extension), $img_extensions)) {
if ($host == "localhost" or $host == "127.0.0.1") { symlink("$fpath/$link_target", "$link_target");} // needed only on my development machine at home
$body .= " \n";
$csv_row_count = 0;
while ($row = fgetcsv($csvfile)) {
$body .= "
\n";
fclose($csvfile);
} else {
if(strcmp($link_target, $link_label) != 0) {
$link_label = $link_label . " ($link_target)";
}
$pad = $blockquote_open ? " " : "" ;
$body .= $pad." \n";
}
} else {
if(strcmp($link_target, $link_label) != 0) {
$link_label = $link_label . " ($link_target)";
}
if($list_open) {$body .= " \n"; $list_open = false;}
$pad = $blockquote_open ? " " : "" ;
$body .= $pad." \n";
}
continue;
}
// == HEADLINES
$head_level = 0;
for($i = 0; $i < $line_len; $i++) {
if($line_untrimmed[$i] == "#") {
$head_level += 1;
} else {
break;
}
}
if($head_level > 0) {
$head_count +=1;
$line = ltrim($line, "# \t");
$body .= "\n ";
$csv_row_count += 1;
foreach ($row as $cell) {
if ($csv_row_count == 1) {
$body .= " \n";
}
$body .= " ".htmlspecialchars($cell)." ";
} else {
$body .= "".htmlspecialchars($cell)." ";
}
}
$body .= "\n";
}
$line = ltrim($line, "* \t");
$body .= "
\n";
}
// == BLOCKQUOTES
if(substr($line_untrimmed, 0, 1) == ">") {
if(!$blockquote_open) {
$blockquote_open = true;
$body .= " \n";
}
$line = ltrim($line, "> \t");
} else if($blockquote_open && $line[0] != ">") {
$blockquote_open = false;
$body .= "
\n";
}
// == PARAGRAPHS
if($line_len > 0) {
if($blockquote_open == true) {
$body .= "
after 2 newlines
if($last_br == $line_no - 1) {
$body .= "
\n";
}
$last_br = $line_no;
}
}
// == CLOSE ANY OPEN ELEMENTS
if($blockquote_open) {
$blockquote_open = false;
$body .= " \n";
}
if($preformatted) {
$preformatted = false;
$body .= "";
}
if($list_open) {
$list_open = false;
$body .= " \n";
}
$toc .= "