add code blocks, remove need for collapse_paragraphs

This commit is contained in:
opFez 2021-07-31 13:58:18 +02:00
parent ebfa64ab0b
commit ece80e2d81
3 changed files with 39 additions and 32 deletions

View File

@ -154,36 +154,21 @@ emit_link(FILE *input, FILE *output)
}
/* collapse paragraphs onto one line */
FILE *
collapse_paragraphs(FILE *input)
/* code blocks */
void
emit_code_block(FILE *input, FILE *output)
{
bool firstline = true;
const char *filename = "/tmp/AAAA";
FILE *w = fopen(filename, "w");
char c, lastc = '\n';
while ((c = fgetc(input)) != EOF) {
if (lastc == '\n' && c != '\n') {
if (!firstline)
fputc(' ', w);
}
else if (lastc == '\n' && c == '\n') {
if (firstline) firstline = false;
fputc(lastc, w);
}
else {
fputc(lastc, w);
}
lastc = c;
fprintf(output, "<pre>");
char c;
while ((c = fgetc(input)) != ']') {
if (c == EOF)
die("Unexpected EOF while converting code block.");
fputc(c, output);
}
fputc(lastc, w);
fclose(w);
return fopen(filename, "r");
fprintf(output, "</pre>");
}
/* main formatter */
void
format(FILE *input, FILE *output)
@ -191,7 +176,10 @@ format(FILE *input, FILE *output)
char c, lastc = '\n';
while ((c = fgetc(input)) != EOF) {
if (c == '*' && lastc == '\n') { /* heading */
if (c == '\\') {
fputc(fgetc(input), output);
}
else if (c == '*' && lastc == '\n') { /* heading */
emit_heading(input, output);
}
else if (c == '{') { /* link */
@ -199,9 +187,15 @@ format(FILE *input, FILE *output)
fprintf(output, "<p>");
emit_link(input, output);
}
else if (lastc == '\n') { /* new paragraph */
else if (c == '[' && lastc == '\n') { /* code block */
emit_code_block(input, output);
}
else if (lastc == '\n' && c == '\n') { /* new paragraph */
fputc('\n', output);
fprintf(output, "<p>");
fputc(c, output);
}
else if (c == '\n') {
fputc(' ', output);
}
else {
fputc(c, output);
@ -227,6 +221,7 @@ main(int argc, char *argv[])
FILE *in_initial = fopen(argv[1], "r");
FILE *in_collapsed = collapse_paragraphs(in_initial);
fclose(in_initial);
FILE *out;
if (argc == 2) {
@ -242,11 +237,12 @@ main(int argc, char *argv[])
out = fopen(argv[2], "w");
}
FILE *in = fopen(argv[1], "r");
write_file(out, "resources/header.html");
format(in_collapsed, out);
format(in, out);
write_file(out, "resources/footer.html");
fclose(in_initial);
fclose(out);
fclose(in_collapsed);
}

View File

@ -12,3 +12,13 @@ This is a normal paragraph. Paragraphs can have newlines anywhere in them to
allow for 80 character lines. They're separated by two consecutive newlines.
This is a second paragraph.
{http://example.com Explicit links can also have their link text broken by
newlines without affecting the output.}
Underneath is a code block.
[
(define (frob x)
(frobnicate x))
]

View File

@ -6,4 +6,5 @@ only has a few distinct formatting rules.
To get an overview over how it works, take a look at the
{../resources/writan-example.txt example document}.
{https://tildegit.org/opfez/codex/branch/master/format.c Source code of the formatter}
{https://tildegit.org/opfez/codex/branch/master/format.c Source code of the
formatter}