From: Michael Göhler Date: Sat, 2 Apr 2016 21:09:51 +0000 (+0200) Subject: added cli param to enable char entity expansion + docu X-Git-Url: https://git.danieliu.xyz/?a=commitdiff_plain;h=ab232c9a47a28d78fd50fb602b7e8d6904f5d38c;p=smdp.git added cli param to enable char entity expansion + docu --- diff --git a/include/parser.h b/include/parser.h index 83ce5d4..db95f65 100644 --- a/include/parser.h +++ b/include/parser.h @@ -49,11 +49,10 @@ #define CODE_INDENT 4 #define UNORDERED_LIST_MAX_LEVEL 3 -deck_t *markdown_load(FILE *input); +deck_t *markdown_load(FILE *input, int noexpand); int markdown_analyse(cstring_t *text, int prev); void markdown_debug(deck_t *deck, int debug); void expand_character_entities(line_t *line); -void setup_character_entities(void); void adjust_line_length(line_t *line); int next_nonblank(cstring_t *text, int i); int prev_blank(cstring_t *text, int i); diff --git a/mdp.1 b/mdp.1 index 6179d61..ba7c24a 100644 --- a/mdp.1 +++ b/mdp.1 @@ -5,7 +5,7 @@ .\" . . -.TH MDP 1 "2016-02-07" "User Commands" +.TH MDP 1 "2016-04-02" "User Commands" .SH NAME mdp \- A command-line based markdown presentation tool @@ -33,6 +33,9 @@ or if the file name is the presentation is read from standard input. .SS "Output Control" .TP +.BR \-e ", " \-\^\-expand +Enable character entity expansion (e.g. '>' becomes '>'). +.TP .BR \-f ", " \-\^\-nofade Disable color fading in 256 color mode. .TP diff --git a/src/main.c b/src/main.c index 19e7687..5aefd75 100644 --- a/src/main.c +++ b/src/main.c @@ -31,6 +31,7 @@ void usage() { fprintf(stderr, "%s", "A command-line based markdown presentation tool.\n\n"); fprintf(stderr, "%s", " -d, --debug enable debug messages on STDERR\n"); fprintf(stderr, "%s", " add it multiple times to increases debug level\n"); + fprintf(stderr, "%s", " -e, --expand enable character entity expansion\n"); fprintf(stderr, "%s", " -f, --nofade disable color fading in 256 color mode\n"); fprintf(stderr, "%s", " -h, --help display this help and exit\n"); fprintf(stderr, "%s", " -i, --invert swap black and white color\n"); @@ -56,18 +57,20 @@ int main(int argc, char *argv[]) { int notrans = 0; // disable transparency int nofade = 0; // disable fading int invert = 0; // invert color (black on white) + int noexpand = 1; // disable character entity expansion int reload = 0; // reload page N (0 means no reload) int noreload = 1; // reload disabled until we know input is a file int slidenum = 2; // 0:don't show; 1:show #; 2:show #/# // define command-line options struct option longopts[] = { - { "debug", no_argument, 0, 'd' }, - { "nofade", no_argument, 0, 'f' }, - { "help", no_argument, 0, 'h' }, - { "invert", no_argument, 0, 'i' }, - { "notrans", no_argument, 0, 't' }, - { "version", no_argument, 0, 'v' }, + { "debug", no_argument, 0, 'd' }, + { "expand", no_argument, 0, 'e' }, + { "nofade", no_argument, 0, 'f' }, + { "help", no_argument, 0, 'h' }, + { "invert", no_argument, 0, 'i' }, + { "notrans", no_argument, 0, 't' }, + { "version", no_argument, 0, 'v' }, { "noslidenum", no_argument, 0, 's' }, { "noslidemax", no_argument, 0, 'x' }, { 0, 0, 0, 0 } @@ -75,14 +78,15 @@ int main(int argc, char *argv[]) { // parse command-line options int opt, debug = 0; - while ((opt = getopt_long(argc, argv, ":dfhitvsx", longopts, NULL)) != -1) { + while ((opt = getopt_long(argc, argv, ":defhitvsx", longopts, NULL)) != -1) { switch(opt) { - case 'd': debug += 1; break; - case 'f': nofade = 1; break; - case 'h': usage(); break; - case 'i': invert = 1; break; - case 't': notrans = 1; break; - case 'v': version(); break; + case 'd': debug += 1; break; + case 'e': noexpand = 0; break; + case 'f': nofade = 1; break; + case 'h': usage(); break; + case 'i': invert = 1; break; + case 't': notrans = 1; break; + case 'v': version(); break; case 's': slidenum = 0; break; case 'x': slidenum = 1; break; case ':': fprintf(stderr, "%s: '%c' requires an argument\n", argv[0], optopt); usage(); break; @@ -98,9 +102,6 @@ int main(int argc, char *argv[]) { // setup list string setup_list_strings(); - // setup character entities - setup_character_entities(); - // open file or set input to STDIN char *file = NULL; FILE *input; @@ -143,7 +144,7 @@ int main(int argc, char *argv[]) { // load deck object from input deck_t *deck; - deck = markdown_load(input); + deck = markdown_load(input, noexpand); // close file fclose(input); diff --git a/src/parser.c b/src/parser.c index f084667..2157310 100644 --- a/src/parser.c +++ b/src/parser.c @@ -31,7 +31,76 @@ #include "parser.h" -deck_t *markdown_load(FILE *input) { +// char entry translation table +static struct named_character_entity { + wchar_t ucs; + const wchar_t *name; +} named_character_entities[] = { + { L'\x0022', L"quot" }, + { L'\x0026', L"amp" }, + { L'\x0027', L"apos" }, + { L'\x003C', L"lt" }, + { L'\x003E', L"gt" }, + { L'\x00A2', L"cent" }, + { L'\x00A3', L"pound" }, + { L'\x00A5', L"yen" }, + { L'\x00A7', L"sect" }, + { L'\x00A9', L"copy" }, + { L'\x00AA', L"laquo" }, + { L'\x00AE', L"reg" }, + { L'\x00B0', L"deg" }, + { L'\x00B1', L"plusmn" }, + { L'\x00B2', L"sup2" }, + { L'\x00B3', L"sup3" }, + { L'\x00B6', L"para" }, + { L'\x00B9', L"sup1" }, + { L'\x00BB', L"raquo" }, + { L'\x00BC', L"frac14" }, + { L'\x00BD', L"frac12" }, + { L'\x00BE', L"frac34" }, + { L'\x00D7', L"times" }, + { L'\x00F7', L"divide" }, + { L'\x2018', L"lsquo" }, + { L'\x2019', L"rsquo" }, + { L'\x201C', L"ldquo" }, + { L'\x201D', L"rdquo" }, + { L'\x2020', L"dagger" }, + { L'\x2021', L"Dagger" }, + { L'\x2022', L"bull" }, + { L'\x2026', L"hellip" }, + { L'\x2030', L"permil" }, + { L'\x2032', L"prime" }, + { L'\x2033', L"Prime" }, + { L'\x2039', L"lsaquo" }, + { L'\x203A', L"rsaquo" }, + { L'\x20AC', L"euro" }, + { L'\x2122', L"trade" }, + { L'\x2190', L"larr" }, + { L'\x2191', L"uarr" }, + { L'\x2192', L"rarr" }, + { L'\x2193', L"darr" }, + { L'\x2194', L"harr" }, + { L'\x21B5', L"crarr" }, + { L'\x21D0', L"lArr" }, + { L'\x21D1', L"uArr" }, + { L'\x21D2', L"rArr" }, + { L'\x21D3', L"dArr" }, + { L'\x21D4', L"hArr" }, + { L'\x221E', L"infin" }, + { L'\x2261', L"equiv" }, + { L'\x2308', L"lceil" }, + { L'\x2309', L"rceil" }, + { L'\x230A', L"lfloor" }, + { L'\x230B', L"rfloor" }, + { L'\x25CA', L"loz" }, + { L'\x2660', L"spades" }, + { L'\x2663', L"clubs" }, + { L'\x2665', L"hearts" }, + { L'\x2666', L"diams" }, + { L'\0', NULL }, +}; + +deck_t *markdown_load(FILE *input, int noexpand) { wchar_t c = L'\0'; // char int i = 0; // increment @@ -122,7 +191,9 @@ deck_t *markdown_load(FILE *input) { line->offset = next_nonblank(text, 0); // expand character entities if enabled - if(line->text->value) + if(line->text->value && + !noexpand && + !CHECK_BIT(line->bits, IS_CODE)) expand_character_entities(line); // adjust line length dynamicaly - excluding markup @@ -607,97 +678,11 @@ void markdown_debug(deck_t *deck, int debug) { } } -static int enable_character_entities = 0; -static struct named_character_entity { - wchar_t ucs; - const wchar_t *name; -} named_character_entities[] = { - { L'\x0022', L"quot" }, - { L'\x0026', L"amp" }, - { L'\x0027', L"apos" }, - { L'\x003C', L"lt" }, - { L'\x003E', L"gt" }, - { L'\x00A2', L"cent" }, - { L'\x00A3', L"pound" }, - { L'\x00A5', L"yen" }, - { L'\x00A7', L"sect" }, - { L'\x00A9', L"copy" }, - { L'\x00AA', L"laquo" }, - { L'\x00AE', L"reg" }, - { L'\x00B0', L"deg" }, - { L'\x00B1', L"plusmn" }, - { L'\x00B2', L"sup2" }, - { L'\x00B3', L"sup3" }, - { L'\x00B6', L"para" }, - { L'\x00B9', L"sup1" }, - { L'\x00BB', L"raquo" }, - { L'\x00BC', L"frac14" }, - { L'\x00BD', L"frac12" }, - { L'\x00BE', L"frac34" }, - { L'\x00D7', L"times" }, - { L'\x00F7', L"divide" }, - { L'\x2018', L"lsquo" }, - { L'\x2019', L"rsquo" }, - { L'\x201C', L"ldquo" }, - { L'\x201D', L"rdquo" }, - { L'\x2020', L"dagger" }, - { L'\x2021', L"Dagger" }, - { L'\x2022', L"bull" }, - { L'\x2026', L"hellip" }, - { L'\x2030', L"permil" }, - { L'\x2032', L"prime" }, - { L'\x2033', L"Prime" }, - { L'\x2039', L"lsaquo" }, - { L'\x203A', L"rsaquo" }, - { L'\x20AC', L"euro" }, - { L'\x2122', L"trade" }, - { L'\x2190', L"larr" }, - { L'\x2191', L"uarr" }, - { L'\x2192', L"rarr" }, - { L'\x2193', L"darr" }, - { L'\x2194', L"harr" }, - { L'\x21B5', L"crarr" }, - { L'\x21D0', L"lArr" }, - { L'\x21D1', L"uArr" }, - { L'\x21D2', L"rArr" }, - { L'\x21D3', L"dArr" }, - { L'\x21D4', L"hArr" }, - { L'\x221E', L"infin" }, - { L'\x2261', L"equiv" }, - { L'\x2308', L"lceil" }, - { L'\x2309', L"rceil" }, - { L'\x230A', L"lfloor" }, - { L'\x230B', L"rfloor" }, - { L'\x25CA', L"loz" }, - { L'\x2660', L"spades" }, - { L'\x2663', L"clubs" }, - { L'\x2665', L"hearts" }, - { L'\x2666', L"diams" }, - { L'\0', NULL }, -}; - -/* -export MDP_ENABLE_CHARENT=1 -*/ -void setup_character_entities(void) -{ - char *str = getenv("MDP_ENABLE_CHARENT"); - if (str == NULL) - enable_character_entities = 0; - else if (str[0] == '\0') - enable_character_entities = 1; - else - enable_character_entities = atoi(str); -} - void expand_character_entities(line_t *line) { wchar_t *ampersand; wchar_t *prev, *curr; - if (!enable_character_entities) - return; - ampersand = NULL; curr = &line->text->value[0];