X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=src%2Fparser.c;h=eb50016874b2e90df59d5cd81f7db8dec1e89355;hb=94a28d11cee6db33d9c456b395ed59f2631b08e5;hp=59b6b529c83dd83b25d3ea8bdc81fed2f94d3021;hpb=faaa6a40adb643fa62ab355b3ab617af1e454fed;p=smdp.git
diff --git a/src/parser.c b/src/parser.c
index 59b6b52..eb50016 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -2,7 +2,7 @@
* Functions necessary to parse a file and transform its content into
* a deck of slides containing lines. All based on markdown formating
* rules.
- * Copyright (C) 2014 Michael Goehler
+ * Copyright (C) 2016 Michael Goehler
*
* This file is part of mdp.
*
@@ -68,6 +68,14 @@ deck_t *markdown_load(FILE *input) {
// clear text
(text->reset)(text);
+ } else if(line && CHECK_BIT(bits, IS_STOP)) {
+
+ // set stop bit on last line
+ SET_BIT(line->bits, IS_STOP);
+
+ // clear text
+ (text->reset)(text);
+
// if text is markdown hr
} else if(CHECK_BIT(bits, IS_HR) &&
CHECK_BIT(line->bits, IS_EMPTY)) {
@@ -81,6 +89,11 @@ deck_t *markdown_load(FILE *input) {
slide = next_slide(slide);
sc++;
+ } else if(CHECK_BIT(bits, IS_TILDE_CODE) &&
+ CHECK_BIT(bits, IS_EMPTY)) {
+ // remove tilde code markers
+ (text->reset)(text);
+
} else {
// if slide ! has line
@@ -183,6 +196,30 @@ deck_t *markdown_load(FILE *input) {
slide = deck->slide;
while(slide) {
line = slide->line;
+
+ // ignore mdpress format attributes
+ if(line &&
+ slide->lines > 1 &&
+ !CHECK_BIT(line->bits, IS_EMPTY) &&
+ line->text->value[line->offset] == L'=' &&
+ line->text->value[line->offset + 1] == L' ') {
+
+ // remove line from linked list
+ slide->line = line->next;
+ line->next->prev = NULL;
+
+ // maintain loop condition
+ tmp = line;
+ line = line->next;
+
+ // adjust line count
+ slide->lines -= 1;
+
+ // delete line
+ (tmp->text->delete)(tmp->text);
+ free(tmp);
+ }
+
while(line) {
// combine underlined H1/H2 in single line
if((CHECK_BIT(line->bits, IS_H1) ||
@@ -287,6 +324,7 @@ int markdown_analyse(cstring_t *text, int prev) {
// the program remembers their value on every function calls
static int unordered_list_level = 0;
static int unordered_list_level_offset[] = {-1, -1, -1, -1};
+ static int num_tilde_characters = 0;
int i = 0; // increment
int bits = 0; // markdown bits
@@ -302,12 +340,47 @@ int markdown_analyse(cstring_t *text, int prev) {
// return IS_EMPTY on null pointers
if(!text || !text->value) {
SET_BIT(bits, IS_EMPTY);
+
+ // continue fenced code blocks across empty lines
+ if(num_tilde_characters > 0)
+ SET_BIT(bits, IS_CODE);
+
return bits;
}
// count leading spaces
offset = next_nonblank(text, 0);
+ // IS_TILDE_CODE
+ if (wcsncmp(text->value, L"~~~", 3) == 0) {
+ int tildes_in_line = next_nontilde(text, 0);
+ if (tildes_in_line >= num_tilde_characters) {
+ if (num_tilde_characters > 0) {
+ num_tilde_characters = 0;
+ } else {
+ num_tilde_characters = tildes_in_line;
+ }
+ SET_BIT(bits, IS_EMPTY);
+ SET_BIT(bits, IS_TILDE_CODE);
+ return bits;
+ }
+ }
+
+ if (num_tilde_characters > 0) {
+ SET_BIT(bits, IS_CODE);
+ SET_BIT(bits, IS_TILDE_CODE);
+ return bits;
+ }
+
+ // IS_STOP
+ if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) &&
+ (!wcsncmp(&text->value[offset], L"
", 4) ||
+ !wcsncmp(&text->value[offset], L"
", 4) ||
+ !wcsncmp(&text->value[offset], L"^", 1))) {
+ SET_BIT(bits, IS_STOP);
+ return bits;
+ }
+
// strip trailing spaces
for(eol = text->size; eol > offset && iswspace(text->value[eol - 1]); eol--);
@@ -381,7 +454,8 @@ int markdown_analyse(cstring_t *text, int prev) {
// IS_CODE
if(offset >= CODE_INDENT &&
(CHECK_BIT(prev, IS_EMPTY) ||
- CHECK_BIT(prev, IS_CODE))) {
+ CHECK_BIT(prev, IS_CODE) ||
+ CHECK_BIT(prev, IS_STOP))) {
SET_BIT(bits, IS_CODE);
} else {
@@ -532,7 +606,7 @@ void markdown_debug(deck_t *deck, int debug) {
void adjust_line_length(line_t *line) {
int l = 0;
const static wchar_t *special = L"\\*_`"; // list of interpreted chars
- const wchar_t *c = &line->text->value[line->offset];
+ const wchar_t *c = &line->text->value[0];
cstack_t *stack = cstack_init();
// for each char in line
@@ -597,3 +671,11 @@ int next_blank(cstring_t *text, int i) {
int next_word(cstring_t *text, int i) {
return next_nonblank(text, next_blank(text, i));
}
+
+int next_nontilde(cstring_t *text, int i) {
+ while ((i < text->size) && text->value[i] == L'~')
+ i++;
+
+ return i;
+}
+