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);
}
*/
// == 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
$file = fopen(".".$_SERVER['REDIRECT_URL'], "r") or die("Not found.");
$fname = $_SERVER['REDIRECT_URL'];
$fmod = date("F d Y H:i:s.", filemtime($fname)); // apparently disabled on hickory.he.net, works fine in development environment
$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
"; } else { $body .= "\n"; } continue; } if($preformatted) { $body .= htmlentities($line_untrimmed); 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)) { // symlink("$fpath/$link_target", "$link_target"); // needed only on my development machine at home $body .= "
$link_label
$link_label
$link_label
".htmlspecialchars($cell)." | "; } else { $body .= "".htmlspecialchars($cell)." | "; } } $body .= "
---|
\n"; } $line = ltrim(parse_line($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($line)."
\n"; } else { $body .= "".parse_line($line)."
\n"; } } else { // == WHITESPACE // only insert
Search this site at marginalia.nu