From 2781f75cd4c18ef718eabefc339af2536a48e3d9 Mon Sep 17 00:00:00 2001 From: Lains Date: Sat, 13 Feb 2021 10:48:02 -0300 Subject: [PATCH] WebView.vala / FileManager.vala - Use YAML'd markdown files to good use Now, if there's YAML in the file, let's use its contents for what they should be used for, and not be dumb about it. --- src/Services/FileManager.vala | 113 ++++++++++++++++++++++++++++++++++ src/Widgets/WebView.vala | 96 ++++++++++------------------- 2 files changed, 144 insertions(+), 65 deletions(-) diff --git a/src/Services/FileManager.vala b/src/Services/FileManager.vala index 7781f890..a5cc2c8b 100644 --- a/src/Services/FileManager.vala +++ b/src/Services/FileManager.vala @@ -122,4 +122,117 @@ namespace Quilter.Services.FileManager { throw e; } } + + public string get_yamlless_markdown ( + string markdown, + int lines, + out string title, + out string date, + bool non_empty = true, + bool include_title = true, + bool include_date = true) + { + string buffer = markdown; + Regex headers = null; + try { + headers = new Regex ("^\\s*(.+)\\s*:\\s+(.*)", RegexCompileFlags.MULTILINE | RegexCompileFlags.CASELESS, 0); + } catch (Error e) { + warning ("Could not compile regex: %s", e.message); + } + + string temp_title = ""; + string temp_date = ""; + + MatchInfo matches; + var markout = new StringBuilder (); + int mklines = 0; + + if (buffer.length > 4 && buffer[0:4] == "---\n") { + int i = 0; + int last_newline = 3; + int next_newline; + bool valid_frontmatter = true; + string line = ""; + + while (valid_frontmatter) { + next_newline = buffer.index_of_char('\n', last_newline + 1); + if (next_newline == -1 && !((buffer.length > last_newline + 1) && buffer.substring (last_newline + 1).has_prefix("---"))) { + valid_frontmatter = false; + break; + } + + if (next_newline == -1) { + line = buffer.substring (last_newline + 1); + } else { + line = buffer[last_newline+1:next_newline]; + } + last_newline = next_newline; + + if (line == "---") { + break; + } + + if (headers != null) { + if (headers.match (line, RegexMatchFlags.NOTEMPTY_ATSTART, out matches)) { + if (matches.fetch (1).ascii_down() == "title") { + temp_title = matches.fetch (2).chug ().chomp (); + if (temp_title.has_prefix ("\"") && temp_title.has_suffix ("\"")) { + temp_title = temp_title.substring (1, temp_title.length - 2); + } + if (include_title) { + markout.append ("# " + temp_title + "\n"); + mklines++; + } + } else if (matches.fetch (1).ascii_down() == "date") { + temp_date = matches.fetch (2).chug ().chomp (); + if (include_date) { + markout.append ("## " + temp_date + "\n"); + mklines++; + } + } + } else { + line = line.down ().chomp (); + if (!line.has_prefix ("-") && line != "") { + valid_frontmatter = false; + break; + } + } + } else { + string quick_parse = line.chomp (); + if (quick_parse.has_prefix ("title")) { + temp_title = quick_parse.substring (quick_parse.index_of (":") + 1); + if (temp_title.has_prefix ("\"") && temp_title.has_suffix ("\"")) { + temp_title = temp_title.substring (1, temp_title.length - 2); + } + if (include_title) { + markout.append ("# " + temp_title); + mklines++; + } + } else if (quick_parse.has_prefix ("date")) { + temp_date = quick_parse.substring (quick_parse.index_of (":") + 1).chug ().chomp (); + if (include_date) { + markout.append ("## " + temp_date); + mklines++; + } + } + } + + i++; + } + + if (!valid_frontmatter) { + markout.erase (); + markout.append (markdown); + } else { + markout.append (buffer[last_newline:buffer.length]); + } + } else { + markout.append (markdown); + } + + title = temp_title; + date = temp_date; + + return markout.str; + } } diff --git a/src/Widgets/WebView.vala b/src/Widgets/WebView.vala index 2ec10da7..4dd557a8 100644 --- a/src/Widgets/WebView.vala +++ b/src/Widgets/WebView.vala @@ -210,78 +210,44 @@ namespace Quilter { } } - /** - * Process the frontmatter of a markdown document, if it exists. - * Returns the frontmatter data and strips the frontmatter from the markdown doc. - * - * @see http://jekyllrb.com/docs/frontmatter/ - */ - private string[] process_frontmatter (string raw_mk, out string processed_mk) { - string[] map = {}; - - processed_mk = null; - - if (raw_mk.length > 4 && raw_mk[0:4] == "---\n") { - int i = 0; - bool valid_frontmatter = true; - int last_newline = 3; - int next_newline; - string line = ""; - while (true) { - next_newline = raw_mk.index_of_char('\n', last_newline + 1); - if (next_newline == -1) { - valid_frontmatter = false; - break; - } - line = raw_mk[last_newline+1:next_newline]; - last_newline = next_newline; - - if (line == "---") { - break; - } - - var sep_index = line.index_of_char(':'); - if (sep_index != -1) { - map += line[0:sep_index-1]; - map += line[sep_index+1:line.length]; - } else { - valid_frontmatter = false; - break; - } - - i++; - } - - if (valid_frontmatter) { - processed_mk = raw_mk[last_newline:raw_mk.length]; - } - } - - if (processed_mk == null) { - processed_mk = raw_mk; - } - - return map; - } - public void update_html_view () { string processed_mk; - process_frontmatter (buf.text, out processed_mk); + string title, date; + processed_mk = Services.FileManager.get_yamlless_markdown( + buf.text, + 0, // Cap number of lines + out title, + out date, + true, // Include empty lines + true, // H1 title: + false // Include date + ); + var mkd = new Markdown.Document.from_gfm_string (processed_mk.data, Markdown.DocumentFlags.TOC + - Markdown.DocumentFlags.AUTOLINK + Markdown.DocumentFlags.EXTRA_FOOTNOTE + - Markdown.DocumentFlags.AUTOLINK + Markdown.DocumentFlags.DLEXTRA + - Markdown.DocumentFlags.FENCEDCODE + Markdown.DocumentFlags.GITHUBTAGS + - Markdown.DocumentFlags.LATEX + Markdown.DocumentFlags.URLENCODEDANCHOR + - Markdown.DocumentFlags.NOSTYLE + Markdown.DocumentFlags.EXPLICITLIST); + Markdown.DocumentFlags.AUTOLINK + + Markdown.DocumentFlags.EXTRA_FOOTNOTE + + Markdown.DocumentFlags.DLEXTRA + + Markdown.DocumentFlags.FENCEDCODE + + Markdown.DocumentFlags.GITHUBTAGS + + Markdown.DocumentFlags.LATEX + + Markdown.DocumentFlags.URLENCODEDANCHOR + + Markdown.DocumentFlags.NOSTYLE + + Markdown.DocumentFlags.EXPLICITLIST + ); mkd.compile ( - Markdown.DocumentFlags.TOC + Markdown.DocumentFlags.AUTOLINK + + Markdown.DocumentFlags.TOC + + Markdown.DocumentFlags.AUTOLINK + Markdown.DocumentFlags.EXTRA_FOOTNOTE + - Markdown.DocumentFlags.AUTOLINK + Markdown.DocumentFlags.DLEXTRA + - Markdown.DocumentFlags.FENCEDCODE + Markdown.DocumentFlags.GITHUBTAGS + - Markdown.DocumentFlags.LATEX + Markdown.DocumentFlags.URLENCODEDANCHOR + - Markdown.DocumentFlags.EXPLICITLIST + Markdown.DocumentFlags.NOSTYLE); + Markdown.DocumentFlags.DLEXTRA + + Markdown.DocumentFlags.FENCEDCODE + + Markdown.DocumentFlags.GITHUBTAGS + + Markdown.DocumentFlags.LATEX + + Markdown.DocumentFlags.URLENCODEDANCHOR + + Markdown.DocumentFlags.NOSTYLE + + Markdown.DocumentFlags.EXPLICITLIST + ); mkd.get_document (out processed_mk); string highlight = set_highlight();