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 .= ""; 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 - the path must be absolute, starting at the root of the filesystem: file:///home/james/sample.gmi $fname = $_GET['page']; // == when running on my local development machine using the PHP web-server $file = fopen($fname, "r") or die("Not found."); // == when running on my local development machine using the PHP web-server $fpath = str_replace("file://", "", pathinfo($fname, $flags = PATHINFO_DIRNAME)); $spath = "file://" . $fpath; $script = $_SERVER['SCRIPT_NAME']; $whoami = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; // HP15 } else { $fname = $_SERVER['CONTEXT_DOCUMENT_ROOT']."/".$_SERVER['REDIRECT_URL']; // == when running on the production server using the Apache web-server $file = fopen(".".$_SERVER['REDIRECT_URL'], "r") or die("Not found."); // == when running on the production server using the Apache web-server $whoami = $_SERVER['SCRIPT_URI']; // hickory.he.net } $fmod = date("F d Y H:i:s.", filemtime($fname)); $line_no = 0; $head_count = 0; $preformatted = false; $blockquote_open = false; $list_open = false; $last_br = -1; $is_pdf = false; $title = ''; $body = ''; $toc = "

Contents of this page:

\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)."\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

View this document as a PDF

\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 .= "
";
        } 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 .= "

 $link_label
\"$link_label\"

\n"; } elseif (in_array(strtolower($extension), $aud_extensions)) { $body .= "

$link_label

\n"; } elseif (in_array(strtolower($extension), $vid_extensions)) { $body .= "

$link_label

\n"; // == INLINE DISPLAY of CSV data } elseif (strtolower($extension) == "csv") { if ($host == "localhost" or $host == "127.0.0.1") { $csvfile = fopen("$fpath/$link_target", "r"); // production server does not need $fpath } else { $csvfile = fopen("$link_target", "r"); // apparently disabled on hickory.he.net, works fine in development environment } if ($csvfile !== false) { $body .= " $link_label\n"; $body .= " \n"; $csv_row_count = 0; while ($row = fgetcsv($csvfile)) { $body .= " "; $csv_row_count += 1; foreach ($row as $cell) { if ($csv_row_count == 1) { $body .= ""; } else { $body .= ""; } } $body .= "\n"; } $body .= "
\n"; fclose($csvfile); } else { if(strcmp($link_target, $link_label) != 0) { $link_label = $link_label . " ($link_target)"; } $pad = $blockquote_open ? " " : "" ; $body .= $pad."

$link_label

\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."

$link_label

\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 ".parse_line($line)."\n\n"; $toc .= " ".parse_line($line)."\n"; if($title == "") { $title = $line; } continue; } // == LISTS if(substr($line_untrimmed, 0, 2) == "* ") { if(!$list_open) { $list_open = true; if($blockquote_open && $line[0] != ">") {$blockquote_open = false;} $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 .= "

".parse_line(htmlentities($line))."

\n"; } else { $body .= "

".parse_line(htmlentities($line))."

\n"; } } else { // == WHITESPACE // only insert
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 .= "

\n"; fclose($file); // == Finally: output the page. inserting its elements in the appropriate places. echo << $title
\n $toc
$body

END; ?>