half the markdown jet

This commit is contained in:
Philip C Monk 2014-12-03 23:58:49 -05:00
parent b24550e981
commit 451e652c40
2 changed files with 202 additions and 6 deletions

View File

@ -79,6 +79,7 @@
# define c3__blin c3_s4('b','l','i','n') # define c3__blin c3_s4('b','l','i','n')
# define c3__blit c3_s4('b','l','i','t') # define c3__blit c3_s4('b','l','i','t')
# define c3__blog c3_s4('b','l','o','g') # define c3__blog c3_s4('b','l','o','g')
# define c3__bloq c3_s4('b','l','o','q')
# define c3__blot c3_s4('b','l','o','t') # define c3__blot c3_s4('b','l','o','t')
# define c3__blue c3_s4('b','l','u','e') # define c3__blue c3_s4('b','l','u','e')
# define c3__blur c3_s4('b','l','u','r') # define c3__blur c3_s4('b','l','u','r')
@ -272,6 +273,7 @@
# define c3__dec c3_s3('d','e','c') # define c3__dec c3_s3('d','e','c')
# define c3__deem c3_s4('d','e','e','m') # define c3__deem c3_s4('d','e','e','m')
# define c3__deep c3_s4('d','e','e','p') # define c3__deep c3_s4('d','e','e','p')
# define c3__defn c3_s4('d','e','f','n')
# define c3__del c3_s3('d','e','l') # define c3__del c3_s3('d','e','l')
# define c3__delc c3_s4('d','e','l','c') # define c3__delc c3_s4('d','e','l','c')
# define c3__delt c3_s4('d','e','l','t') # define c3__delt c3_s4('d','e','l','t')
@ -339,6 +341,7 @@
# define c3__edit c3_s4('e','d','i','t') # define c3__edit c3_s4('e','d','i','t')
# define c3__elm c3_s3('e','l','m') # define c3__elm c3_s3('e','l','m')
# define c3__else c3_s4('e','l','s','e') # define c3__else c3_s4('e','l','s','e')
# define c3__emph c3_s4('e','m','p','h')
# define c3__end c3_s3('e','n','d') # define c3__end c3_s3('e','n','d')
# define c3__eq c3_s2('e','q') # define c3__eq c3_s2('e','q')
# define c3__esh c3_s3('e','s','h') # define c3__esh c3_s3('e','s','h')
@ -497,6 +500,7 @@
# define c3__hosc c3_s4('h','o','s','c') # define c3__hosc c3_s4('h','o','s','c')
# define c3__hose c3_s4('h','o','s','e') # define c3__hose c3_s4('h','o','s','e')
# define c3__howl c3_s4('h','o','w','l') # define c3__howl c3_s4('h','o','w','l')
# define c3__hrul c3_s4('h','r','u','l')
# define c3__hsbn c3_s4('h','s','b','n') # define c3__hsbn c3_s4('h','s','b','n')
# define c3__hsbr c3_s4('h','s','b','r') # define c3__hsbr c3_s4('h','s','b','r')
# define c3__hscn c3_s4('h','s','c','n') # define c3__hscn c3_s4('h','s','c','n')
@ -510,6 +514,7 @@
# define c3__hsts c3_s4('h','s','t','s') # define c3__hsts c3_s4('h','s','t','s')
# define c3__htcn c3_s4('h','t','c','n') # define c3__htcn c3_s4('h','t','c','n')
# define c3__htls c3_s4('h','t','l','s') # define c3__htls c3_s4('h','t','l','s')
# define c3__html c3_s4('h','t','m','l')
# define c3__http c3_s4('h','t','t','p') # define c3__http c3_s4('h','t','t','p')
# define c3__hume c3_s4('h','u','m','e') # define c3__hume c3_s4('h','u','m','e')
# define c3__hunk c3_s4('h','u','n','k') # define c3__hunk c3_s4('h','u','n','k')
@ -530,6 +535,7 @@
# define c3__inuk c3_s4('i','n','u','k') # define c3__inuk c3_s4('i','n','u','k')
# define c3__iron c3_s4('i','r','o','n') # define c3__iron c3_s4('i','r','o','n')
# define c3__is c3_s2('i','s') # define c3__is c3_s2('i','s')
# define c3__item c3_s4('i','t','e','m')
# define c3__ix c3_s2('i','x') # define c3__ix c3_s2('i','x')
# define c3__jack c3_s4('j','a','c','k') # define c3__jack c3_s4('j','a','c','k')
# define c3__jamx c3_s4('j','a','m','x') # define c3__jamx c3_s4('j','a','m','x')
@ -610,6 +616,7 @@
# define c3__lint c3_s4('l','i','n','t') # define c3__lint c3_s4('l','i','n','t')
# define c3__liqd c3_s4('l','i','q','d') # define c3__liqd c3_s4('l','i','q','d')
# define c3__lisc c3_s4('l','i','s','c') # define c3__lisc c3_s4('l','i','s','c')
# define c3__list c3_s4('l','i','s','t')
# define c3__lite c3_s4('l','i','t','e') # define c3__lite c3_s4('l','i','t','e')
# define c3__live c3_s4('l','i','v','e') # define c3__live c3_s4('l','i','v','e')
# define c3__load c3_s4('l','o','a','d') # define c3__load c3_s4('l','o','a','d')
@ -743,6 +750,7 @@
# define c3__pane c3_s4('p','a','n','e') # define c3__pane c3_s4('p','a','n','e')
# define c3__pang c3_s4('p','a','n','g') # define c3__pang c3_s4('p','a','n','g')
# define c3__pank c3_s4('p','a','n','k') # define c3__pank c3_s4('p','a','n','k')
# define c3__para c3_s4('p','a','r','a')
# define c3__parq c3_s4('p','a','r','q') # define c3__parq c3_s4('p','a','r','q')
# define c3__part c3_s4('p','a','r','t') # define c3__part c3_s4('p','a','r','t')
# define c3__pass c3_s4('p','a','s','s') # define c3__pass c3_s4('p','a','s','s')

View File

@ -3,21 +3,209 @@
** This file is in the public domain. ** This file is in the public domain.
*/ */
#include "all.h" #include "all.h"
#include <cmark.h>
#include <node.h>
u3_noun node_to_noun(cmark_node * nod);
u3_noun list_elems_to_noun(cmark_node * nod)
{
u3_noun elems = u3_nul;
cmark_node * child;
for ( child = nod->last_child; child; child = child->prev ) {
elems = u3nc(node_to_noun(child),elems);
}
return elems;
}
u3_noun document_to_noun(cmark_node * nod)
{
return list_elems_to_noun(nod);
}
u3_noun block_quote_to_noun(cmark_node * nod)
{
return u3nc(u3nc(c3__bloq,u3_nul),list_elems_to_noun(nod));
}
u3_noun list_to_noun(cmark_node * nod)
{
return
u3nc(
u3nt(
c3__list,
__(nod->as.list.tight),
(nod->as.list.list_type == CMARK_BULLET_LIST)
? nod->as.list.bullet_char /* XX convert? */
: u3nc(nod->as.list.start,
(nod->as.list.delimiter == CMARK_PERIOD_DELIM)
? '.'
: ')')),
list_elems_to_noun(nod));
}
u3_noun list_item_to_noun(cmark_node * nod)
{
return u3nc(u3nc(c3__item,u3_nul),list_elems_to_noun(nod));
}
u3_noun code_block_to_noun(cmark_node * nod)
{
u3_atom str = u3i_string(nod->string_content.ptr); /* XX u3i_bytes */
u3_noun res =
u3nt(
c3__code,
nod->as.code.fenced
? u3nq(
u3_nul,
nod->as.code.fence_char,
nod->as.code.fence_length,
u3i_tape(nod->as.code.info.ptr)
)
: u3_nul,
u3qe_lore(str));
u3z(str);
return res;
}
u3_noun html_to_noun(cmark_node * nod)
{
u3_atom str = u3i_string(nod->string_content.ptr); /* XX u3i_bytes */
u3_noun res = u3nc(c3__html, u3qe_lore(str));
u3z(str);
return res;
}
u3_noun paragraph_to_noun(cmark_node * nod)
{
return u3nc(c3__para, list_elems_to_noun(nod));
}
u3_noun header_to_noun(cmark_node * nod)
{
/* see also nod->as.header.setext */
return u3nt(c3__head, nod->as.header.level, list_elems_to_noun(nod));
}
u3_noun hrule_to_noun(cmark_node * nod)
{
return u3nc(c3__hrul, u3_nul);
}
u3_noun reference_def_to_noun(cmark_node * nod)
{
return u3nc(c3__defn, u3_nul);
}
u3_noun text_to_noun(cmark_node * nod)
{
return u3nc(u3_blip, u3i_tape(cmark_chunk_to_cstr(&nod->as.literal)));
}
u3_noun softbreak_to_noun(cmark_node * nod) // XXX
{
return u3nt(0, 10, 0);
}
u3_noun linebreak_to_noun(cmark_node * nod)
{
return u3nc(c3__line, u3_nul);
}
u3_noun inline_code_to_noun(cmark_node * nod)
{
return u3nc(c3__code, u3i_tape(cmark_chunk_to_cstr(&nod->as.literal)));
}
u3_noun inline_html_to_noun(cmark_node * nod) // XXX
{
return text_to_noun(nod);
}
u3_noun emph_to_noun(cmark_node * nod)
{
return u3nc(u3nc(c3__emph, c3n), list_elems_to_noun(nod));
}
u3_noun strong_to_noun(cmark_node * nod)
{
return u3nc(u3nc(c3__emph, c3y), list_elems_to_noun(nod));
}
u3_noun link_to_noun(cmark_node * nod)
{
return
u3nc(
u3nt(
c3__link,
u3i_tape(nod->as.link.url),
nod->as.link.title
? u3nc(u3_nul, u3i_tape(nod->as.link.title))
: u3_nul),
list_elems_to_noun(nod));
}
u3_noun image_to_noun(cmark_node * nod)
{
return
u3nc(
u3nt(
c3__blot,
u3i_tape(nod->as.link.url),
nod->as.link.title
? u3nc(u3_nul, u3i_tape(nod->as.link.title))
: u3_nul),
list_elems_to_noun(nod));
}
u3_noun node_to_noun(cmark_node * nod)
{
if (!nod) {
fprintf(stderr, "markdown null node");
return u3m_bail(c3__fail);
}
switch ( nod->type ) {
/* Block */
case CMARK_NODE_DOCUMENT: return document_to_noun(nod);
case CMARK_NODE_BLOCK_QUOTE: return block_quote_to_noun(nod);
case CMARK_NODE_LIST: return list_to_noun(nod);
case CMARK_NODE_LIST_ITEM: return list_item_to_noun(nod);
case CMARK_NODE_CODE_BLOCK: return code_block_to_noun(nod);
case CMARK_NODE_HTML: return html_to_noun(nod);
case CMARK_NODE_PARAGRAPH: return paragraph_to_noun(nod);
case CMARK_NODE_HEADER: return header_to_noun(nod);
case CMARK_NODE_HRULE: return hrule_to_noun(nod);
case CMARK_NODE_REFERENCE_DEF: return reference_def_to_noun(nod);
/* Inline */
case CMARK_NODE_TEXT: return text_to_noun(nod);
case CMARK_NODE_SOFTBREAK: return softbreak_to_noun(nod);
case CMARK_NODE_LINEBREAK: return linebreak_to_noun(nod);
case CMARK_NODE_INLINE_CODE: return inline_code_to_noun(nod);
case CMARK_NODE_INLINE_HTML: return inline_html_to_noun(nod);
case CMARK_NODE_EMPH: return emph_to_noun(nod);
case CMARK_NODE_STRONG: return strong_to_noun(nod);
case CMARK_NODE_LINK: return link_to_noun(nod);
case CMARK_NODE_IMAGE: return image_to_noun(nod);
default: fprintf(stderr, "bad markdown parsing");
return u3m_bail(c3__fail);
}
}
/* functions /* functions
*/ */
u3_noun u3_noun
u3qg_sqar(u3_atom a) u3qg_sqar(u3_atom a)
{ {
mpz_t a_mp; c3_c *tex = u3r_string(a);
fprintf(stderr, "C squared %d!\r\n", a); /* XX better strlen */
cmark_node * doc = cmark_parse_document(tex, strlen(tex));
u3r_mp(a_mp, a); u3_noun res = document_to_noun(doc);
mpz_mul(a_mp, a_mp, a_mp); // free out, tex?
return res;
return u3i_mp(a_mp);
} }
u3_noun u3_noun
u3wg_sqar(u3_noun cor) u3wg_sqar(u3_noun cor)