Skip to content

Instantly share code, notes, and snippets.

@nothings

nothings/foo.c Secret

Created November 19, 2021 21:39
Embed
What would you like to do?
enum
{
MODE_text,
MODE_html,
MODE_latex
};
FILE *file_for_mode[3];
void print_header(Header *h)
{
int mode;
for (mode=0; mode < 3; ++mode) {
FILE *f = file_for_mode[mode];
switch (mode) {
case MODE_text:
fprintf(f, "%s\n%s\n%s\n%s\n%s\n\n", h->title, h->date, h->author, h->subtitle, h->subsubtitle);
break;
case MODE_html:
fprintf(f, "<html><head><title>%s</title></head><body>\n", h->title);
fprintf(f, "<h1>%s<h1>\n<h2>%s</h2>\n<h3>%s</h3>\n<h4>%s</h4>\n<h4>%s</h4>\n", h->title, h->date, h->author, h->subtitle, h->subsubtitle);
break;
case MODE_latex:
fprintf(f, "\\documentclass[12pt ,letterpaper, twoside]{memoir}\n");
fprintf(f, "\\usepackage[utf8]{inputenc}\n");
fprintf(f, "\\usepackage{geometry}\n");
fprintf(f, "\\begin{document}\n");
fprintf(f, "\\newgeometry{left=1cm,right=1cm,top=1cm,bottom=1cm}\n");
fprintf(f, "\\centering\n");
fprintf(f, "\\vspace*{\\baselineskip}\n");
fprintf(f, "\\vspace*{0.167\\textheight}\n");
fprintf(f, "{\\LARGE %s}\\\\[\\baselineskip]\n", h->title);
fprintf(f, "{\\LARGE %s}\\\\[\\baselineskip]\n", h->date);
fprintf(f, "{\\Large %s}\\\\[\\baselineskip]\n", h->author);
fprintf(f, "{\\large %s}\\\\", h->subtitle);
fprintf(f, "{\\large %s}\\\\", h->subsubtitle);
fprintf(f, "\\vfill\n");
fprintf(f, "%s\\par\n", h->publisher);
fprintf(f, "\\vspace*{3\\baselineskip}\n");
fprintf(f, "\\restoregeometry\n");
fprintf(f, "\\frontmatter\n");
fprintf(f, "\\mainmatter\n");
break;
}
}
}
void print_text(char *text)
{
char *s;
char *escape_chars[3] = {
"",
"&<>",
"&%$#_{}|~^\\"
};
char *escape_substitutions[3][20] = {
{ "" },
{ "&amp;", "&lt;", "&gt;" },
{ "\\&", "\\%", "\\$", "\\#", "\\_", "\\{", "\\}", "{\\textbar}", "{\\textasciitilde}", "{\\textasciicircum}", "{\\textbackslash}" }
};
int mode;
int in_italics = 0;
for (mode=0; mode < 3; ++mode) {
char *esc_list;
FILE *f = file_for_mode[mode];
esc_list = escape_chars[mode];
s = text;
while (*s) {
char *enter_whitespace = s;
// at this point, we have whitespace or are at start
// compress multiple whitespace to one, and delete whitespace before punctuation
while (isspace(*s))
++s;
if (!(s[0] == '.' && islower(s[1]))) {
if (s != enter_whitespace)
fputc(' ', f);
} else {
// process dot command
switch (s[1]) {
case 'i':
in_italics = !in_italics;
switch (mode) {
case MODE_text : fprintf(f, "_"); break;
case MODE_html : fprintf(f, in_italics ? "<i>" : "</i>"); break;
case MODE_latex: fprintf(f, in_italics ? "\\textit{" : "}"); break;
}
break;
case 'p':
switch (mode) {
case MODE_text : fprintf(f, "\n\n"); break;
case MODE_html : fprintf(f, "<p>"); break;
case MODE_latex: fprintf(f, "\\par"); break;
}
break;
case 'l':
switch (mode) {
case MODE_text : fprintf(f, "\n"); break;
case MODE_html : fprintf(f, "<br>"); break;
case MODE_latex: fprintf(f, "\\\\"); break;
}
break;
}
s += 2;
}
while (*s && !isspace(*s)) {
char *z = strchr(esc_list, *s);
if (z != NULL)
fprintf(f, "%s", escape_substitutions[mode][z - esc_list]);
else
fputc(*s, f);
s += 1;
}
}
}
}
void print_footer(void)
{
int mode;
for (mode=0; mode < 3; ++mode) {
FILE *f = file_for_mode[mode];
switch (mode) {
case MODE_text:
break;
case MODE_html:
fprintf(f, "</body></html>\n");
break;
case MODE_latex:
fprintf(f, "\\end{document}\n");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment