if (startsWith(line, "```")) { // ``` -> preformatted text
preformattedMode = !preformattedMode; // toggle global mode
if (!preformattedMode)
return replaceWord(line, "```", "
");
if (strlen(line) < 5)
return "";
return join3s("");
}
if (preformattedMode) // while global mode
return replaceWord(line, "
", ""); // -> revert preProcess
//
if (startsWith(line, "> ")) { // > -> blockquote (escaped)
line = replaceWord(line, "
", ""); // -> revert preProcess
return join3s("", &line[5], "
\n");
}
//
if (startsWith(line, "=>")) // => link (escaped)
return toLink(line); // ->
if (startsWith(line, "### ")) // ### -> h3
return join3s("", &line[4], "
");
if (startsWith(line, "## ")) // ## -> h2
return join3s("", &line[3], "
");
if (startsWith(line, "# ")) { // # -> h1 ...AND set global page title!
if (areSame(title, DEFAULT_TITLE)) // (stick to first h1 found)
title = join3s("", &line[2], "\n");
return join3s("", &line[2], "
");
}
//
if (areSame(line, "---"))
return "
";
//
number length;
number ticksCount =
countWord(line, "`"); // futile attempt to prevent bleeding
if (ticksCount > 1 && ticksCount % 2 == 0) { // simple check, no guarantees
line = replaceWord(line, " `", " "); // start inline code
line = replaceWord(line, "` ", "
"); // end inline code
if (startsWith(line, "`")) // if first thin in the line
line = join2s("", &line[1]); // begin with inline code
length = strlen(line);
if (length > 0 && line[length - 1] == '`') // if last...
line = replaceWord(line, "`", "
"); // end inline code
}
// (accepts inline code)
if (startsWith(line, "* ")) { // * -> list item
itemsMode = true; // toggle global mode
return join3s("
", &line[2], ""); // end inline bold text
}
// ...or else -> simple text
return line;
}
// try to get the file from path and generate the corresponding html in place
void convert(string path) {
if (startsWith(path, "-lang:")) {
htmlTag = join3s("\n"); // set langCode
return;
}
if (countWord(path, ".gmi")) { // ultra-simple path validadion
string text = readFile(path); // read and hope content is text/gemini :D
if (text) { // safe-gard / read failure
title = DEFAULT_TITLE; // reset global 'page title' for each file
preformattedMode = false; // reset global ' mode' for each file
linkCount = 0; // reset global 'link count' for each file
bool wasItemsMode = false;
stringList lines = preProcess(text);
number linesCount = listCount(lines);
for (number i = 0; i < linesCount; i++) {
if (itemsMode && areSame(lines[i], "
"))
continue;
itemsMode = false; // reset global 'items mode' for each file
lines[i] = lineToHTML(lines[i]); // do the magic :D
if (itemsMode && !wasItemsMode)
lines[i] = join2s("
\n", lines[i]);
if (wasItemsMode && !itemsMode)
lines[i] = join2s("
\n", lines[i]);
wasItemsMode = itemsMode;
}
string newPath = replaceWord(path, ".gmi", ".html");
if (writeFile(postProcess(linesCount, lines), newPath)) // and save...
return println(newPath);
}
println(join2s(path, " -> FAILED!")); // ...or complain!
}
}
app({
if (argc < 2)
println("please, provide '.gmi' file paths as arguments...");
else
forEach(argc - 1, &argv[1], &convert);
})