Permalink
Browse files

Restructure the "not officially, honest" published __mkd__ function

that does the work;  it's now __mkd_compile() [setup + compile], which
compiles the input, sets up a MMIOT, then returns a pointer to the
compiled Paragraph tree, and __mkd_cleanup() which cleans up all of
the data structures created by __mkd_compile().
Also add a title argument to mkd_dump() so I can show the tree rooted
at a title (the filename of the input file, or `stdin` if none was
given.)
  • Loading branch information...
1 parent c831a82 commit cae8ac3f8c537942fa69c6c158c231607b9e365b david parsons committed Jan 8, 2008
Showing with 81 additions and 52 deletions.
  1. +43 −30 dumptree.c
  2. +1 −1 main.c
  3. +36 −20 markdown.c
  4. +1 −1 mkdio.h
View
@@ -7,8 +7,8 @@ struct frame {
char c;
};
-static STRING(struct frame) stack;
-
+typedef STRING(struct frame) Stack;
+
static char *
Pptype(int typ)
{
@@ -29,64 +29,64 @@ Pptype(int typ)
}
static void
-pushpfx(int indent, char c)
+pushpfx(int indent, char c, Stack *sp)
{
- struct frame *q = &EXPAND(stack);
+ struct frame *q = &EXPAND(*sp);
q->indent = indent;
q->c = c;
}
static void
-poppfx()
+poppfx(Stack *sp)
{
- S(stack)--;
+ S(*sp)--;
}
static void
-changepfx(char c)
+changepfx(Stack *sp, char c)
{
char ch;
- if ( !S(stack) ) return;
+ if ( !S(*sp) ) return;
- ch = T(stack)[S(stack)-1].c;
+ ch = T(*sp)[S(*sp)-1].c;
if ( ch == '+' || ch == '|' )
- T(stack)[S(stack)-1].c = c;
+ T(*sp)[S(*sp)-1].c = c;
}
static void
-printpfx()
+printpfx(Stack *sp)
{
int i;
char c;
- if ( !S(stack) ) return;
+ if ( !S(*sp) ) return;
- c = T(stack)[S(stack)-1].c;
+ c = T(*sp)[S(*sp)-1].c;
if ( c == '+' || c == '-' ) {
printf("--%c", c);
- T(stack)[S(stack)-1].c = (c == '-') ? ' ' : '|';
+ T(*sp)[S(*sp)-1].c = (c == '-') ? ' ' : '|';
}
else
- for ( i=0; i < S(stack); i++ ) {
+ for ( i=0; i < S(*sp); i++ ) {
if ( i )
printf(" ");
- printf("%*s%c", T(stack)[i].indent + 2, " ", T(stack)[i].c);
- if ( T(stack)[i].c == '`' )
- T(stack)[i].c = ' ';
+ printf("%*s%c", T(*sp)[i].indent + 2, " ", T(*sp)[i].c);
+ if ( T(*sp)[i].c == '`' )
+ T(*sp)[i].c = ' ';
}
printf("--");
}
static void
-dumptree(Paragraph *pp, void *context)
+dumptree(Paragraph *pp, Stack *sp)
{
int count;
Line *p;
@@ -95,8 +95,8 @@ dumptree(Paragraph *pp, void *context)
while ( pp ) {
if ( !pp->next )
- changepfx('`');
- printpfx();
+ changepfx(sp, '`');
+ printpfx(sp);
d = printf("[%s", Pptype(pp->typ));
if ( pp->align )
@@ -111,23 +111,36 @@ dumptree(Paragraph *pp, void *context)
d += printf("]");
if ( pp->down ) {
- pushpfx(d, pp->down->next ? '+' : '-');
- dumptree(pp->down, context);
- poppfx();
+ pushpfx(d, pp->down->next ? '+' : '-', sp);
+ dumptree(pp->down, sp);
+ poppfx(sp);
}
else putchar('\n');
pp = pp->next;
}
}
+extern Paragraph *__mkd_compile(Line*, FILE*, int, MMIOT*);
+extern void __mkd_cleanup(Paragraph *, MMIOT*);
+
+
int
-mkd_dump(Line *input, FILE *output, int flags)
+mkd_dump(Line *input, FILE *output, int flags, char *title)
{
- typedef void (*emitter)(Paragraph*,void*);
- extern int __mkd_internal_markdown(Line*, FILE*, int, emitter);
+ Stack stack;
+ MMIOT frame;
+ Paragraph *tree;
+
+ if (( tree = __mkd_compile(input,output,flags,&frame) )) {
+
+ CREATE(stack);
+ pushpfx(printf("%s", title), tree->next ? '+' : '-', &stack);
+ dumptree(tree, &stack);
+ DELETE(stack);
- CREATE(stack);
- return __mkd_internal_markdown(input,output,flags,dumptree);
- DELETE(stack);
+ __mkd_cleanup(tree, &frame);
+ return 0;
+ }
+ return -1;
}
View
@@ -41,7 +41,7 @@ main(int argc, char **argv)
exit(1);
}
if ( debug )
- mkd_dump(mkd_in(stdin), stdout, 0);
+ mkd_dump(mkd_in(stdin), stdout, 0, argc ? basename(argv[0]) : "stdin");
else
markdown(mkd_in(stdin), stdout, 0);
exit(0);
View
@@ -1478,31 +1478,39 @@ mkd_text(char *bfr, int size, FILE *output, int flags)
* the guts of the markdown() function, ripped out so I can do
* debugging.
*/
-typedef void (*emitter)(Paragraph *, MMIOT*);
-
-int
-__mkd_internal_markdown(Line *text, FILE *out, int flags, emitter emit)
+
+/*
+ * prepare and compile `text`, returning a Paragraph tree.
+ */
+Paragraph *
+__mkd_compile(Line *text, FILE *out, int flags, MMIOT *ctx)
{
Paragraph *paragraph;
- MMIOT f;
- bzero(&f, sizeof f);
- f.out = out;
- f.flags = flags;
- CREATE(f.in);
- CREATE(f.footnotes);
+ bzero(ctx, sizeof *ctx);
+ ctx->out = out;
+ ctx->flags = flags;
+ CREATE(ctx->in);
+ CREATE(ctx->footnotes);
initmarkdown();
- paragraph = compile(text, 1, &f);
- qsort(T(f.footnotes), S(f.footnotes), sizeof T(f.footnotes)[0],
- (stfu)footsort);
- (*emit)(paragraph, &f);
+ paragraph = compile(text, 1, ctx);
+ qsort(T(ctx->footnotes), S(ctx->footnotes), sizeof T(ctx->footnotes)[0],
+ (stfu)footsort);
- freefootnotes(&f);
- if ( paragraph ) freeParagraph(paragraph);
- DELETE(f.in);
- return 0;
+ return paragraph;
+}
+
+
+/* clean up everything allocated in __mkd_compile()
+ */
+void
+__mkd_cleanup(Paragraph *tree, MMIOT *ctx)
+{
+ freefootnotes(ctx);
+ if ( tree ) freeParagraph(tree);
+ DELETE(ctx->in);
}
@@ -1511,6 +1519,14 @@ __mkd_internal_markdown(Line *text, FILE *out, int flags, emitter emit)
int
markdown(Line *text, FILE *out, int flags)
{
- return __mkd_internal_markdown(text,out,flags,emit);
-}
+ MMIOT f;
+ Paragraph *pp;
+
+ if (( pp = __mkd_compile(text, out, flags, &f) )) {
+ emit(pp, &f);
+ __mkd_cleanup(pp, &f);
+ return 0;
+ }
+ return -1;
+}
View
@@ -7,7 +7,7 @@
*/
int mkd_text(char *, int, FILE*, int); /* mark up a line of text */
int markdown(void*, FILE*, int); /* mark it on down */
-int mkd_dump(void*, FILE*, int); /* (debug) dump a parse tree */
+int mkd_dump(void*, FILE*, int, char*); /* (debug) dump a parse tree */
/* line builder for markdown()
*/

0 comments on commit cae8ac3

Please sign in to comment.