//*****************************************************************
//
// $file: main.cpp $
// $author: Martin Fouilleul $
// $date: 23/12/2017 $
//
//*****************************************************************
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Martin Fouilleul
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/
#include \n");
while(t->type != ENDF)
{
switch(t->type)
{
case ESC:
EvalEsc(t);
break;
case MARKUP:
if( t->markup.type == CODE_BLOCK_START
|| t->markup.type == MATHS_BLOCK_START
|| t->markup.type == HTML_BLOCK_START
|| t->markup.type == SECTION
|| t->markup.type == LIST)
{
fprintf(output, "\n
");
break;
case THINSPACE:
fprintf(output, " ");
break;
case ALPHA:
fprintf(output, "α");
break;
case BETA:
fprintf(output, "β");
break;
case GAMMA:
fprintf(output, "γ");
break;
case DELTA:
fprintf(output, "δ");
break;
case EPSILON:
fprintf(output, "ε");
break;
case ZETA:
fprintf(output, "ζ");
break;
case ETA:
fprintf(output, "η");
break;
case THETA:
fprintf(output, "θ");
break;
case IOTA:
fprintf(output, "ι");
break;
case KAPPA:
fprintf(output, "κ");
break;
case LAMBDA:
fprintf(output, "λ");
break;
case MU:
fprintf(output, "μ");
break;
case NU:
fprintf(output, "ν");
break;
case XI:
fprintf(output, "ξ");
break;
case OMICRON:
fprintf(output, "ο");
break;
case PI:
fprintf(output, "π");
break;
case RHO:
fprintf(output, "ρ");
break;
case SIGMA:
fprintf(output, "σ");
break;
case TAU:
fprintf(output, "τ");
break;
case UPSILON:
fprintf(output, "υ");
break;
case PHI:
fprintf(output, "φ");
break;
case CHI:
fprintf(output, "χ");
break;
case PSI:
fprintf(output, "ψ");
break;
case OMEGA:
fprintf(output, "ω");
break;
case ALPHA_UPPER:
fprintf(output, "Α");
break;
case BETA_UPPER:
fprintf(output, "Β");
break;
case GAMMA_UPPER:
fprintf(output, "Γ");
break;
case DELTA_UPPER:
fprintf(output, "Δ");
break;
case EPSILON_UPPER:
fprintf(output, "Ε");
break;
case ZETA_UPPER:
fprintf(output, "Ζ");
break;
case ETA_UPPER:
fprintf(output, "Η");
break;
case THETA_UPPER:
fprintf(output, "Θ");
break;
case IOTA_UPPER:
fprintf(output, "Ι");
break;
case KAPPA_UPPER:
fprintf(output, "Κ");
break;
case LAMBDA_UPPER:
fprintf(output, "Λ");
break;
case MU_UPPER:
fprintf(output, "Μ");
break;
case NU_UPPER:
fprintf(output, "Ν");
break;
case XI_UPPER:
fprintf(output, "Ξ");
break;
case OMICRON_UPPER:
fprintf(output, "Ο");
break;
case PI_UPPER:
fprintf(output, "Π");
break;
case RHO_UPPER:
fprintf(output, "Ρ");
break;
case SIGMA_UPPER:
fprintf(output, "Σ");
break;
case TAU_UPPER:
fprintf(output, "Τ");
break;
case UPSILON_UPPER:
fprintf(output, "Υ");
break;
case PHI_UPPER:
fprintf(output, "Φ");
break;
case CHI_UPPER:
fprintf(output, "Χ");
break;
case PSI_UPPER:
fprintf(output, "Ψ");
break;
case OMEGA_UPPER:
fprintf(output, "Ω");
break;
}
}
void EvalURL(token* t)
{
fprintf(output, "", t->markup.text);
GetNextToken(input, t);
while(t->type != ENDF)
{
switch(t->type)
{
case ESC:
EvalEsc(t);
break;
case MARKUP:
if( t->markup.type == CODE_BLOCK_START
|| t->markup.type == MATHS_BLOCK_START
|| t->markup.type == HTML_BLOCK_START
|| t->markup.type == LIST)
{
//error
return;
}
else if(t->markup.type == URL_STOP)
{
fprintf(output, "\n");
return;
}
else
{
EvalMarkup(t);
}
break;
case HTML_TAG:
EvalTag(t);
break;
case ENDL:
fprintf(output, "
\n");
break;
case SYMBOL:
EvalSymbol(t);
break;
case CHAR: default:
EvalChar(t);
break;
}
GetNextToken(input, t);
}
//error
}
void EvalImage(token* t)
{
fprintf(output, "", t->markup.text);
}
void EvalMathsChar(token* t)
{
//NOTE(martin): replaces special characters with html entities
switch(t->c)
{
case '<':
fprintf(output, "<");
break;
case '>':
fprintf(output, ">");
break;
case '&':
fprintf(output, "&");
break;
case '\t':
fprintf(output, " ");
break;
case '+': case '/' : case '*': case '!': case '=':
fprintf(output, " %c ", t->c);
break;
case '(': case ')' : case '[' : case ']': case ',': case ';':
fprintf(output, "%c", t->c);
break;
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
fprintf(output, "%c", t->c);
break;
default:
if(t->c >= 'A' && t->c <= 'Z' && !italic)
{
fprintf(output, "%c", t->c);
}
else
{
putc(t->c, output);
}
break;
}
}
void EvalMathsSymbol(token* t)
{
switch(t->symbol.code)
{
case MULTIPLY:
fprintf(output, " × ");
break;
case DIVIDE:
fprintf(output, " ÷ ");
break;
case SUBSTRACT:
fprintf(output, " − ");
break;
case STAR:
fprintf(output, " * ");
break;
case LOSTAR:
fprintf(output, " ∗ ");
break;
case CDOT:
fprintf(output, " · ");
break;
case LEQ:
fprintf(output, "≤");
break;
case GEQ:
fprintf(output, "≥");
break;
case INF:
fprintf(output, "∞");
break;
case OTIMES:
fprintf(output, " ⊗ ");
break;
case LARR:
fprintf(output, "←");
break;
case RARR:
fprintf(output, "→");
break;
case EMDASH:
fprintf(output, "—");
break;
case LINE:
fprintf(output, "
");
break;
case THINSPACE:
fprintf(output, " ");
break;
case ALPHA:
fprintf(output, "α");
break;
case BETA:
fprintf(output, "β");
break;
case GAMMA:
fprintf(output, "γ");
break;
case DELTA:
fprintf(output, "δ");
break;
case EPSILON:
fprintf(output, "ε");
break;
case ZETA:
fprintf(output, "ζ");
break;
case ETA:
fprintf(output, "η");
break;
case THETA:
fprintf(output, "θ");
break;
case IOTA:
fprintf(output, "ι");
break;
case KAPPA:
fprintf(output, "κ");
break;
case LAMBDA:
fprintf(output, "λ");
break;
case MU:
fprintf(output, "μ");
break;
case NU:
fprintf(output, "ν");
break;
case XI:
fprintf(output, "ξ");
break;
case OMICRON:
fprintf(output, "ο");
break;
case PI:
fprintf(output, "π");
break;
case RHO:
fprintf(output, "ρ");
break;
case SIGMA:
fprintf(output, "σ");
break;
case TAU:
fprintf(output, "τ");
break;
case UPSILON:
fprintf(output, "υ");
break;
case PHI:
fprintf(output, "φ");
break;
case CHI:
fprintf(output, "χ");
break;
case PSI:
fprintf(output, "ψ");
break;
case OMEGA:
fprintf(output, "ω");
break;
case ALPHA_UPPER:
fprintf(output, "Α");
break;
case BETA_UPPER:
fprintf(output, "Β");
break;
case GAMMA_UPPER:
fprintf(output, "Γ");
break;
case DELTA_UPPER:
fprintf(output, "Δ");
break;
case EPSILON_UPPER:
fprintf(output, "Ε");
break;
case ZETA_UPPER:
fprintf(output, "Ζ");
break;
case ETA_UPPER:
fprintf(output, "Η");
break;
case THETA_UPPER:
fprintf(output, "Θ");
break;
case IOTA_UPPER:
fprintf(output, "Ι");
break;
case KAPPA_UPPER:
fprintf(output, "Κ");
break;
case LAMBDA_UPPER:
fprintf(output, "Λ");
break;
case MU_UPPER:
fprintf(output, "Μ");
break;
case NU_UPPER:
fprintf(output, "Ν");
break;
case XI_UPPER:
fprintf(output, "Ξ");
break;
case OMICRON_UPPER:
fprintf(output, "Ο");
break;
case PI_UPPER:
fprintf(output, "Π");
break;
case RHO_UPPER:
fprintf(output, "Ρ");
break;
case SIGMA_UPPER:
fprintf(output, "Σ");
break;
case TAU_UPPER:
fprintf(output, "Τ");
break;
case UPSILON_UPPER:
fprintf(output, "Υ");
break;
case PHI_UPPER:
fprintf(output, "Φ");
break;
case CHI_UPPER:
fprintf(output, "Χ");
break;
case PSI_UPPER:
fprintf(output, "Ψ");
break;
case OMEGA_UPPER:
fprintf(output, "Ω");
break;
}
}
void EvalEsc(token* t)
{
switch(t->c)
{
case '\n':
fprintf(output, "\n");
break;
case 'n':
fprintf(output, "
\n");
break;
case 't':
fprintf(output, " ");
break;
case 's': case ' ':
fprintf(output, " ");
break;
case '&':
putc('&', output);
break;
default:
EvalChar(t);
break;
}
}
void EvalSection(token* t)
{
int sectionLevel = t->markup.value;
fprintf(output, "
\n");
break;
case SYMBOL:
EvalSymbol(t);
break;
case CHAR: default:
EvalChar(t);
break;
}
GetNextToken(input, t);
}
}
void EvalBold()
{
if(!bold)
{
fprintf(output, "");
bold = true;
}
else
{
fprintf(output, "");
bold = false;
}
}
void EvalItalic()
{
if(!italic)
{
fprintf(output, "");
italic = true;
}
else
{
fprintf(output, "");
italic = false;
}
}
void EvalUnderline()
{
if(!underline)
{
fprintf(output, "");
underline = true;
}
else
{
fprintf(output, "");
underline = false;
}
}
void EvalRegular()
{
if(!regular)
{
fprintf(output, "");
regular = true;
}
else
{
fprintf(output, "");
regular = false;
}
}
void EvalCode(token* t, bool inlineCode)
{
if(inlineCode || (t->markup.text && !strcmp(t->markup.text, "none")))
{
fprintf(output, "<%s class=\"code\">", inlineCode ? "span" : "div");
GetNextToken(input, t);
bool exit = false;
while(t->type != ENDF && !exit)
{
switch(t->type)
{
case MARKUP:
{
if(t->markup.type != CODE_INLINE_STOP)
{
//error;
}
exit = true;
}
break;
case CHAR:
{
EvalCharInCodeMode(t);
}
break;
default:
//error
exit = true;
break;
}
if(!exit)
{
GetNextToken(input, t);
}
}
fprintf(output, "%s>", inlineCode ? "span" : "div");
}
else
{
fprintf(output, "
\n");
break;
case SYMBOL:
EvalMathsSymbol(&t);
break;
case CHAR: default:
EvalMathsChar(&t);
break;
}
GetNextToken(input, &t);
}
if(inlineMaths)
{
fprintf(output, "");
}
else
{
fprintf(output, "
\n");
}
continue;
}
}
break;
case SYMBOL:
EvalSymbol(&t);
break;
case CHAR: default:
EvalChar(&t);
break;
}
if(!exit)
{
GetNextToken(input, &t);
}
}
if(t.type == MARKUP && t.markup.type == LIST)
{
if(t.markup.value >= listLevel)
{
if(t.markup.value == listLevel)
{
fprintf(output, "