pinosaur
/
smdp.git
/ blobdiff
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
UTF-8 sample presentation for testing purposes
[smdp.git]
/
src
/
parser.c
diff --git
a/src/parser.c
b/src/parser.c
index
760ef4b
..
4d48e33
100644
(file)
--- a/
src/parser.c
+++ b/
src/parser.c
@@
-25,13
+25,15
@@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
+#include <wchar.h>
+#include <wctype.h>
#include <string.h>
#include "parser.h"
deck_t *markdown_load(FILE *input) {
#include <string.h>
#include "parser.h"
deck_t *markdown_load(FILE *input) {
-
int c = 0
; // char
+
wchar_t c = L'\0'
; // char
int i = 0; // increment
int hc = 0; // header count
int lc = 0; // line count
int i = 0; // increment
int hc = 0; // header count
int lc = 0; // line count
@@
-48,13
+50,13
@@
deck_t *markdown_load(FILE *input) {
// initialize bits as empty line
SET_BIT(bits, IS_EMPTY);
// initialize bits as empty line
SET_BIT(bits, IS_EMPTY);
- while ((c = fget
c(input)) !=
EOF) {
+ while ((c = fget
wc(input)) != W
EOF) {
if (ferror(input)) {
fprintf(stderr, "markdown_load() failed to read input: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (ferror(input)) {
fprintf(stderr, "markdown_load() failed to read input: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
- if(c == '\n') {
+ if(c ==
L
'\n') {
// markdown analyse
prev = bits;
// markdown analyse
prev = bits;
@@
-82,7
+84,7
@@
deck_t *markdown_load(FILE *input) {
} else {
// if slide ! has line
} else {
// if slide ! has line
- if(!slide->line) {
+ if(!slide->line
|| !line
) {
// create new line
line = new_line();
// create new line
line = new_line();
@@
-114,14
+116,14
@@
deck_t *markdown_load(FILE *input) {
text = cstring_init();
}
text = cstring_init();
}
- } else if(c == '\t') {
+ } else if(c ==
L
'\t') {
// expand tab to spaces
for (i = 0; i < EXPAND_TABS; i++) {
// expand tab to spaces
for (i = 0; i < EXPAND_TABS; i++) {
- (text->expand)(text, ' ');
+ (text->expand)(text,
L
' ');
}
}
- } else if(c == '\\') {
+ } else if(c ==
L
'\\') {
// add char to line
(text->expand)(text, c);
// add char to line
(text->expand)(text, c);
@@
-130,64
+132,52
@@
deck_t *markdown_load(FILE *input) {
// and do not increase line count
if(next_nonblank(text, 0) < CODE_INDENT) {
// and do not increase line count
if(next_nonblank(text, 0) < CODE_INDENT) {
- c = fgetc(input);
+ c = fget
w
c(input);
(text->expand)(text, c);
(text->expand)(text, c);
-
- if(is_utf8(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);
- }
- }
-
}
}
- } else if(is
print(c) || isspace((unsigned char)
c)) {
+ } else if(is
wprint(c) || iswspace(
c)) {
// add char to line
(text->expand)(text, c);
// add char to line
(text->expand)(text, c);
-
- } 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);
- }
}
}
}
}
+ (text->delete)(text);
slide->lines = lc;
deck->slides = sc;
// detect header
line = deck->slide->line;
slide->lines = lc;
deck->slides = sc;
// detect header
line = deck->slide->line;
- if(line && line->text->size > 0 && line->text->text[0] == '%') {
+ if(line && line->text->size > 0 && line->text->text[0] ==
L
'%') {
// assign header to deck
deck->header = line;
// find first non-header line
// assign header to deck
deck->header = line;
// find first non-header line
- while(line
->text->size > 0 && line->text->text[0] ==
'%') {
+ while(line
&& line->text->size > 0 && line->text->text[0] == L
'%') {
hc++;
line = line->next;
}
hc++;
line = line->next;
}
- // split linked list
- line->prev->next = NULL;
- line->prev = NULL;
+ // only split header if any non-header line is found
+ if(line) {
+
+ // split linked list
+ line->prev->next = NULL;
+ line->prev = NULL;
- // remove header lines from slide
- deck->slide->line = line;
+
// remove header lines from slide
+
deck->slide->line = line;
- // adjust counts
- deck->headers += hc;
- deck->slide->lines -= hc;
+ // adjust counts
+ deck->headers += hc;
+ deck->slide->lines -= hc;
+ } else {
+
+ // remove header from deck
+ deck->header = NULL;
+ }
}
slide = deck->slide;
}
slide = deck->slide;
@@
-319,12
+309,12
@@
int markdown_analyse(cstring_t *text, int prev) {
offset = next_nonblank(text, 0);
// strip trailing spaces
offset = next_nonblank(text, 0);
// strip trailing spaces
- for(eol = text->size; eol > offset && is
space((unsigned char)
text->text[eol - 1]); eol--);
+ for(eol = text->size; eol > offset && is
wspace(
text->text[eol - 1]); eol--);
// IS_UNORDERED_LIST_#
if(text->size >= offset + 2 &&
// IS_UNORDERED_LIST_#
if(text->size >= offset + 2 &&
- (text->text[offset] ==
'*' || text->text[offset] ==
'-') &&
-
text->text[offset + 1] == ' '
) {
+ (text->text[offset] ==
L'*' || text->text[offset] == L
'-') &&
+
iswspace(text->text[offset + 1])
) {
// if different from last lines offset
if(offset != unordered_list_offset) {
// if different from last lines offset
if(offset != unordered_list_offset) {
@@
-397,15
+387,15
@@
int markdown_analyse(cstring_t *text, int prev) {
} else {
// IS_QUOTE
} else {
// IS_QUOTE
- if(text->text[offset] == '>') {
+ if(text->text[offset] ==
L
'>') {
SET_BIT(bits, IS_QUOTE);
}
// IS_CENTER
if(text->size >= offset + 3 &&
SET_BIT(bits, IS_QUOTE);
}
// IS_CENTER
if(text->size >= offset + 3 &&
- text->text[offset] == '-' &&
- text->text[offset + 1] == '>' &&
-
text->text[offset + 2] == ' '
) {
+ text->text[offset] ==
L
'-' &&
+ text->text[offset + 1] ==
L
'>' &&
+
iswspace(text->text[offset + 2])
) {
SET_BIT(bits, IS_CENTER);
// remove start tag
SET_BIT(bits, IS_CENTER);
// remove start tag
@@
-413,31
+403,31
@@
int markdown_analyse(cstring_t *text, int prev) {
eol -= 3;
if(text->size >= offset + 3 &&
eol -= 3;
if(text->size >= offset + 3 &&
- text->text[eol - 1] == '-' &&
- text->text[eol - 2] == '<' &&
-
text->text[eol - 3] == ' '
) {
+ text->text[eol - 1] ==
L
'-' &&
+ text->text[eol - 2] ==
L
'<' &&
+
iswspace(text->text[eol - 3])
) {
// remove end tags
(text->strip)(text, eol - 3, 3);
// adjust end of line
// remove end tags
(text->strip)(text, eol - 3, 3);
// adjust end of line
- for(eol = text->size; eol > offset && is
space((unsigned char)
text->text[eol - 1]); eol--);
+ for(eol = text->size; eol > offset && is
wspace(
text->text[eol - 1]); eol--);
}
}
for(i = offset; i < eol; i++) {
}
}
for(i = offset; i < eol; i++) {
- if(
text->text[i] == ' '
) {
+ if(
iswspace(text->text[i])
) {
spaces++;
} else {
switch(text->text[i]) {
spaces++;
} else {
switch(text->text[i]) {
- case '=': equals++; break;
- case '#': hashes++; break;
- case '*': stars++; break;
- case '-': minus++; break;
- case '\\': other++; i++; break;
+ case
L
'=': equals++; break;
+ case
L
'#': hashes++; break;
+ case
L
'*': stars++; break;
+ case
L
'-': minus++; break;
+ case
L
'\\': other++; i++; break;
default: other++; break;
}
}
default: other++; break;
}
}
@@
-445,23
+435,23
@@
int markdown_analyse(cstring_t *text, int prev) {
// IS_H1
if(equals > 0 &&
// IS_H1
if(equals > 0 &&
-
hashes + stars + minus + spaces + other == 0) {
+ hashes + stars + minus + spaces + other == 0) {
SET_BIT(bits, IS_H1);
}
SET_BIT(bits, IS_H1);
}
- if(text->text[offset] == '#' &&
-
text->text[offset+1] == ' '
) {
+ if(text->text[offset] ==
L
'#' &&
+
iswspace(text->text[offset+1])
) {
SET_BIT(bits, IS_H1);
SET_BIT(bits, IS_H1_ATX);
}
// IS_H2
if(minus > 0 &&
SET_BIT(bits, IS_H1);
SET_BIT(bits, IS_H1_ATX);
}
// IS_H2
if(minus > 0 &&
-
equals + hashes + stars + spaces + other == 0) {
+ equals + hashes + stars + spaces + other == 0) {
SET_BIT(bits, IS_H2);
}
SET_BIT(bits, IS_H2);
}
- if(text->text[offset] == '#' &&
-
text->text[offset+1] ==
'#' &&
-
text->text[offset+2] == ' '
) {
+ if(text->text[offset] ==
L
'#' &&
+
text->text[offset+1] == L
'#' &&
+
iswspace(text->text[offset+2])
) {
SET_BIT(bits, IS_H2);
SET_BIT(bits, IS_H2_ATX);
}
SET_BIT(bits, IS_H2);
SET_BIT(bits, IS_H2_ATX);
}
@@
-492,7
+482,7
@@
void markdown_debug(deck_t *deck, int debug) {
line_t *header;
if(debug == 1) {
line_t *header;
if(debug == 1) {
- f
printf(stderr,
"headers: %i\nslides: %i\n", deck->headers, deck->slides);
+ f
wprintf(stderr, L
"headers: %i\nslides: %i\n", deck->headers, deck->slides);
} else if(debug > 1) {
} else if(debug > 1) {
@@
-501,12
+491,12
@@
void markdown_debug(deck_t *deck, int debug) {
header = deck->header;
while(header &&
header->length > 0 &&
header = deck->header;
while(header &&
header->length > 0 &&
- header->text->text[0] == '%') {
+ header->text->text[0] ==
L
'%') {
// skip descriptor word (e.g. %title:)
offset = next_blank(header->text, 0) + 1;
// skip descriptor word (e.g. %title:)
offset = next_blank(header->text, 0) + 1;
- f
printf(stderr, "header: %s
\n", &header->text->text[offset]);
+ f
wprintf(stderr, L"header: %S
\n", &header->text->text[offset]);
header = header->next;
}
}
header = header->next;
}
}
@@
-520,17
+510,17
@@
void markdown_debug(deck_t *deck, int debug) {
sc++;
if(debug == 1) {
sc++;
if(debug == 1) {
- f
printf(stderr,
" slide %i: %i lines\n", sc, slide->lines);
+ f
wprintf(stderr, L
" slide %i: %i lines\n", sc, slide->lines);
} else if(debug > 1) {
// also print bits and line length
} else if(debug > 1) {
// also print bits and line length
- f
printf(stderr,
" slide %i:\n", sc);
+ f
wprintf(stderr, L
" slide %i:\n", sc);
line = slide->line;
lc = 0;
while(line) {
lc++;
line = slide->line;
lc = 0;
while(line) {
lc++;
- f
printf(stderr,
" line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
+ f
wprintf(stderr, L
" line %i: bits = %i, length = %i\n", lc, line->bits, line->length);
line = line->next;
}
}
line = line->next;
}
}
@@
-541,22
+531,22
@@
void markdown_debug(deck_t *deck, int debug) {
void adjust_line_length(line_t *line) {
int l = 0;
void adjust_line_length(line_t *line) {
int l = 0;
- const static
char *special =
"\\*_`"; // list of interpreted chars
- const
char
*c = &line->text->text[line->offset];
+ const static
wchar_t *special = L
"\\*_`"; // list of interpreted chars
+ const
wchar_t
*c = &line->text->text[line->offset];
cstack_t *stack = cstack_init();
// for each char in line
for(; *c; c++) {
// if char is in special char list
cstack_t *stack = cstack_init();
// for each char in line
for(; *c; c++) {
// if char is in special char list
- if(
str
chr(special, *c)) {
+ if(
wcs
chr(special, *c)) {
// closing special char (or second backslash)
if((stack->top)(stack, *c)) {
// closing special char (or second backslash)
if((stack->top)(stack, *c)) {
- if(*c == '\\') l++;
+ if(*c ==
L
'\\') l++;
(stack->pop)(stack);
// treat special as regular char
(stack->pop)(stack);
// treat special as regular char
- } else if((stack->top)(stack, '\\')) {
+ } else if((stack->top)(stack,
L
'\\')) {
l++;
(stack->pop)(stack);
l++;
(stack->pop)(stack);
@@
-567,7
+557,7
@@
void adjust_line_length(line_t *line) {
} else {
// remove backslash from stack
} else {
// remove backslash from stack
- if((stack->top)(stack, '\\'))
+ if((stack->top)(stack,
L
'\\'))
(stack->pop)(stack);
l++;
}
(stack->pop)(stack);
l++;
}
@@
-583,38
+573,22
@@
void adjust_line_length(line_t *line) {
(stack->delete)(stack);
}
(stack->delete)(stack);
}
-bool is_utf8(char ch) {
- return (ch & 0x80) != 0x00;
-}
-
-int length_utf8(char ch) {
-
- int i = 0; // increment
-
- while(is_utf8(ch)) {
- i++;
- ch <<= 1;
- }
-
- return i;
-}
-
int next_nonblank(cstring_t *text, int i) {
int next_nonblank(cstring_t *text, int i) {
- while ((i < text->size) && is
space((unsigned char)
(text->text)[i]))
+ while ((i < text->size) && is
wspace(
(text->text)[i]))
i++;
return i;
}
int prev_blank(cstring_t *text, int i) {
i++;
return i;
}
int prev_blank(cstring_t *text, int i) {
- while ((i > 0) && !is
space((unsigned char)
(text->text)[i]))
+ while ((i > 0) && !is
wspace(
(text->text)[i]))
i--;
return i;
}
int next_blank(cstring_t *text, int i) {
i--;
return i;
}
int next_blank(cstring_t *text, int i) {
- while ((i < text->size) && !is
space((unsigned char)
(text->text)[i]))
+ while ((i < text->size) && !is
wspace(
(text->text)[i]))
i++;
return i;
i++;
return i;