"; 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; } return $line_out; } // END parse_line $file = fopen(".".$_SERVER['REDIRECT_URL'], "r") or die("Not found."); $fname = $_SERVER['REDIRECT_URL']; $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"); $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"; $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; } // == THEMATIC BREAK - horizontal rule; ⁂ "Asterism" character, see https://en.wikipedia.org/wiki/Dinkus if(substr($line, 0, 3) == "---" || $line == "⁂" ) { $body .= "
\n"; continue; } // == PREFORMATTED if($line_len >= 3 && substr($line, 0, 3) == "```") { if($blockquote_open && $line[0] != ">") {$blockquote_open = false;} $preformatted = !$preformatted; if($preformatted) { $body .= "
";
        } 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 = 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\"

\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") { $csvfile = fopen("$link_target", "r"); 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)"; } $body .= "

$link_label

\n"; } } else { if(strcmp($link_target, $link_label) != 0) { $link_label = $link_label . " ($link_target)"; } $body .= "

$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 .= " $line\n"; $toc .= " $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($line_untrimmed[0] == ">") { if(!$blockquote_open) { $blockquote_open = true; $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
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); echo << $title
\n $toc
$body

Mastodon social.vivaldi.net Mastodon social.linux.pizza Mastodon qoto.org 🌐 jdcard.tilde.team

Search this site at marginalia.nu


END; ?>