Skip to content

Commit

Permalink
WebView.vala / FileManager.vala - Use YAML'd markdown files to good use
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lains committed Feb 13, 2021
1 parent ea92342 commit 2781f75
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 65 deletions.
113 changes: 113 additions & 0 deletions src/Services/FileManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
96 changes: 31 additions & 65 deletions src/Widgets/WebView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 2781f75

Please sign in to comment.