From: Namhyung Kim Date: Sun, 3 Sep 2017 14:22:41 +0000 (+0900) Subject: support Github style code formatting (#121) X-Git-Url: https://git.danieliu.xyz/?p=smdp.git;a=commitdiff_plain;h=b56994fd240a45f3b4060883f6a17c659c495e23 support Github style code formatting (#121) Signed-off-by: Namhyung Kim --- diff --git a/include/markdown.h b/include/markdown.h index 07268dc..e5f3ca5 100644 --- a/include/markdown.h +++ b/include/markdown.h @@ -48,6 +48,7 @@ enum line_bitmask { IS_QUOTE, IS_CODE, IS_TILDE_CODE, + IS_GFM_CODE, IS_HR, IS_UNORDERED_LIST_1, IS_UNORDERED_LIST_2, diff --git a/include/parser.h b/include/parser.h index db95f65..b2ecc21 100644 --- a/include/parser.h +++ b/include/parser.h @@ -59,5 +59,6 @@ int prev_blank(cstring_t *text, int i); int next_blank(cstring_t *text, int i); int next_word(cstring_t *text, int i); int next_nontilde(cstring_t *text, int i); +int next_nonbacktick(cstring_t *text, int i); #endif // !defined( PARSER_H ) diff --git a/sample.md b/sample.md index 4e304c6..502bb2b 100644 --- a/sample.md +++ b/sample.md @@ -115,8 +115,8 @@ at least as many or more ~ for closing. becomes ~~~ {.numberLines} -int main(int argc, char \*argv[]) { - printf("%s\\n", "Hello world!"); +int main(int argc, char *argv[]) { + printf("%s\n", "Hello world!"); } ~~~~~~~~~~~~~~~~~~ @@ -127,6 +127,30 @@ will be ignored -> # Supported markdown formatting <- +You can also use [github](https://guides.github.com/features/mastering-markdown/#GitHub-flavored-markdown) flavored markdown's +code block. Use at least three backticks to open +and at least as many or more backticks for closing. + +\``` +\int main(int argc, char \*argv[]) { +\ printf("%s\\n", "Hello world!"); +\} +\``` + +becomes + +``` +int main(int argc, char *argv[]) { + printf("%s\n", "Hello world!"); +} +``` + +Language hint will be ignored + +------------------------------------------------- + +-> # Supported markdown formatting <- + Quotes are auto-detected by preceding *>*. Multiple *>* are interpreted as nested quotes. diff --git a/src/parser.c b/src/parser.c index 465bdc1..615cc6e 100644 --- a/src/parser.c +++ b/src/parser.c @@ -158,7 +158,8 @@ deck_t *markdown_load(FILE *input, int noexpand) { slide = next_slide(slide); sc++; - } else if(CHECK_BIT(bits, IS_TILDE_CODE) && + } else if((CHECK_BIT(bits, IS_TILDE_CODE) || + CHECK_BIT(bits, IS_GFM_CODE)) && CHECK_BIT(bits, IS_EMPTY)) { // remove tilde code markers (text->reset)(text); @@ -400,6 +401,7 @@ int markdown_analyse(cstring_t *text, int prev) { static int unordered_list_level = 0; static int unordered_list_level_offset[] = {-1, -1, -1, -1}; static int num_tilde_characters = 0; + static int num_backticks = 0; int i = 0; // increment int bits = 0; // markdown bits @@ -447,6 +449,27 @@ int markdown_analyse(cstring_t *text, int prev) { return bits; } + // IS_GFM_CODE + if (wcsncmp(text->value, L"```", 3) == 0) { + int backticks_in_line = next_nonbacktick(text, 0); + if (backticks_in_line >= num_backticks) { + if (num_backticks > 0) { + num_backticks = 0; + } else { + num_backticks = backticks_in_line; + } + SET_BIT(bits, IS_EMPTY); + SET_BIT(bits, IS_GFM_CODE); + return bits; + } + } + + if (num_backticks > 0) { + SET_BIT(bits, IS_CODE); + SET_BIT(bits, IS_GFM_CODE); + return bits; + } + // IS_STOP if((offset < CODE_INDENT || !CHECK_BIT(prev, IS_CODE)) && (!wcsncmp(&text->value[offset], L"
", 4) || @@ -831,3 +854,10 @@ int next_nontilde(cstring_t *text, int i) { return i; } +int next_nonbacktick(cstring_t *text, int i) { + while ((i < text->size) && text->value[i] == L'`') + i++; + + return i; +} + diff --git a/src/viewer.c b/src/viewer.c index c1a4543..20d496d 100644 --- a/src/viewer.c +++ b/src/viewer.c @@ -656,7 +656,8 @@ void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colo // IS_CODE if(CHECK_BIT(line->bits, IS_CODE)) { - if (!CHECK_BIT(line->bits, IS_TILDE_CODE)) { + if (!CHECK_BIT(line->bits, IS_TILDE_CODE) && + !CHECK_BIT(line->bits, IS_GFM_CODE)) { // set static offset for code offset = CODE_INDENT; }