From: Michael Göhler Date: Sun, 17 Aug 2014 11:13:48 +0000 (+0200) Subject: added header detection + test X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=c8c3b3d8b2755bddb5c9562ead12d659c31f3090;p=smdp.git added header detection + test --- diff --git a/include/markdown.h b/include/markdown.h index ee6dff7..4d5105c 100644 --- a/include/markdown.h +++ b/include/markdown.h @@ -49,6 +49,7 @@ page_t *next_page(page_t *prev); document_t *new_document(); int is_utf8(char ch); int next_nonblank(cstring_t *text, int i); +int next_blank(cstring_t *text, int i); document_t *markdown_load(FILE *input); #endif // !defined( MARKDOWN_H ) diff --git a/markdown_io.c b/markdown_io.c index 58c7f3a..ba2ff7a 100644 --- a/markdown_io.c +++ b/markdown_io.c @@ -74,7 +74,25 @@ document_t *markdown_load(FILE *input) { } } - //TODO detect header + // detect header + line = doc->page->line; + if(line && line->text->size > 0 && line->text->text[0] == '%') { + + // assign header to document + doc->header = line; + + // find first non-header line + while(line->text->size > 0 && line->text->text[0] == '%') { + line = line->next; + } + + // split linked list + line->prev->next = (void*)0; + line->prev = (void*)0; + + // remove header lines from page + doc->page->line = line; + } return doc; } @@ -133,3 +151,9 @@ int next_nonblank(cstring_t *text, int i) { return i; }; +int next_blank(cstring_t *text, int i) { + while ((i < text->size) && !isspace((text->text)[i])) + ++i; + return i; +}; + diff --git a/sample.md b/sample.md index fa4bad7..dfb2d1a 100644 --- a/sample.md +++ b/sample.md @@ -1,6 +1,6 @@ -@title: Sample Presentation -@author: Michael Göhler -@date: 2014-08-07 +%title: Sample Presentation +%author: Michael Göhler +%date: 2014-08-07 Title ===== diff --git a/tmp.c b/tmp.c index b908834..805cb3e 100644 --- a/tmp.c +++ b/tmp.c @@ -27,6 +27,19 @@ int main(int argc, char *argv[]) { doc = markdown_load(input); // test line/page load + int offset; + line_t *header; + if(doc->header) { + header = doc->header; + while(header && + header->text->size > 0 && + header->text->text[0] == '%') { + + offset = next_blank(header->text, 0) + 1; + printf("header: %s\n", &header->text->text[offset]); + header = header->next; + } + } int cp = 0, cl = 0; page_t *page = doc->page; line_t *line;