document_t *markdown_load(FILE *input) {
- int c = 0, i = 0, bits = 0;
+ int c = 0, i = 0, l = 0, bits = 0;
document_t *doc;
page_t *page;
// clear text
(text->reset)(text);
+
+ // reset line length
+ l = 0;
+
// create next page
page = next_page(page);
// add bits to line
line->bits = bits;
+ // add length to line
+ line->length = l;
+
// calc offset
line->offset = next_nonblank(text, 0);
// new text
text = cstring_init();
+
+ // reset line length
+ l = 0;
}
} else if(c == '\t') {
// expand tab to spaces
- for (i = 0; i <= 4; i++)
+ for (i = 0; i <= 4; i++) {
(text->expand)(text, ' ');
+ l++;
+ }
+
+ } else if(isprint(c) || isspace(c)) {
+
+ // add char to line
+ (text->expand)(text, c);
- } else if(isprint(c) || isspace(c) || is_utf8(c)) {
+ // increase line lenght
+ l++;
+
+ } else if(is_utf8(c)) {
// add char to line
(text->expand)(text, c);
+
+ // if utf-8 char > 1 byte add remaing to line
+ for(i = 0; i < length_utf8(c) - 1; i++) {
+ c = fgetc(input);
+ (text->expand)(text, c);
+ }
+
+ // increase line length
+ l++;
}
}
return bits;
}
+void markdown_debug(document_t *doc, int debug) {
+
+ // print header to STDERR
+ int offset;
+ line_t *header;
+ if(doc->header) {
+ header = doc->header;
+ while(header &&
+ header->length > 0 &&
+ header->text->text[0] == '%') {
+
+ offset = next_blank(header->text, 0) + 1;
+ fprintf(stderr, "header: %s\n", &header->text->text[offset]);
+ header = header->next;
+ }
+ }
+
+ // print page/line count to STDERR
+ int cp = 0, cl = 0;
+ page_t *page = doc->page;
+ line_t *line;
+ while(page) {
+ cp++;
+ if(debug > 1) {
+ fprintf(stderr, "page %i:\n", cp);
+ }
+ line = page->line;
+ cl = 0;
+ while(line) {
+ cl++;
+ if(debug > 1) {
+ fprintf(stderr, " line %i: bits = %i, length = %i\n", cl, line->bits, line->length);
+ }
+ line = line->next;
+ }
+ if(debug == 1) {
+ fprintf(stderr, "page %i: %i lines\n", cp, cl);
+ }
+ page = page->next;
+ }
+}
+
+
int is_utf8(char ch) {
return (ch & 0x80);
}
+int length_utf8(char ch) {
+ int i = 0;
+ while(ch & 0x80) {
+ i++;
+ ch <<= 1;
+ }
+ return i;
+}
+
int next_nonblank(cstring_t *text, int i) {
while ((i < text->size) && isspace((text->text)[i]))
++i;