From 1fc9b09ebc3384782474652400a0c2e657288e63 Mon Sep 17 00:00:00 2001 From: Nicolas Cuervo Date: Wed, 3 Jun 2020 23:50:04 +0200 Subject: [PATCH] Add enrich_li_class for nested tasklist This intends to generate tasklist the same way vimwiki does for .wiki extensions. Allows for a visualization of progress in nested tasklists, and customization of the same via html templates --- example_files/default.tpl | 39 +++++++++++++++++++++ lib/vimwiki_markdown/wiki_body.rb | 18 ++++++++-- spec/lib/vimwiki_markdown/wiki_body_spec.rb | 30 ++++++++++++++++ spec/spec_helper.rb | 22 ++++++++++++ 4 files changed, 107 insertions(+), 2 deletions(-) diff --git a/example_files/default.tpl b/example_files/default.tpl index 76bbd76..e54be46 100644 --- a/example_files/default.tpl +++ b/example_files/default.tpl @@ -4,6 +4,45 @@ %title% %pygments% + +

%title%

diff --git a/lib/vimwiki_markdown/wiki_body.rb b/lib/vimwiki_markdown/wiki_body.rb index c55c65b..f26d7da 100644 --- a/lib/vimwiki_markdown/wiki_body.rb +++ b/lib/vimwiki_markdown/wiki_body.rb @@ -25,8 +25,9 @@ def to_s HTML::Pipeline::SyntaxHighlightFilter, HTML::Pipeline::TableOfContentsFilter ], { scope: "highlight"}) - result = pipeline.call(html) - result[:output].to_s + @result = pipeline.call(html) + @result = @result[:output].to_s + enrich_li_class! end @@ -74,4 +75,17 @@ def hack_replace_commonmarker_proc! CommonMarker.render_html(content, commonmarker_opts, commonmarker_exts) } end + + def enrich_li_class! + syms = '\ .oOX\]' + syms_hash = {" ]" => 0, ".]" => 1, "o]" => 2, "O]" => 3, "X]" => 4} + checkbox = /
  • \s*\[[\s.oOX]\]/ + checkbox_start = /
  • \s*\[/ + @result.gsub!(checkbox) do |m| + m = m.sub(checkbox_start, '
  • ' + end + @result + end + end diff --git a/spec/lib/vimwiki_markdown/wiki_body_spec.rb b/spec/lib/vimwiki_markdown/wiki_body_spec.rb index 637fd05..cf5988f 100644 --- a/spec/lib/vimwiki_markdown/wiki_body_spec.rb +++ b/spec/lib/vimwiki_markdown/wiki_body_spec.rb @@ -52,6 +52,36 @@ module VimwikiMarkdown expect(wiki_body.to_s).to match(/there<\/a>/) end + it "must enrich task list unchecked" do + allow_any_instance_of(VimwikiMarkdown::VimwikiLink).to receive(:vimwiki_markdown_file_exists?).and_return(true) + allow(wiki_body).to receive(:get_wiki_markdown_contents).and_return('- [ ] This is one line') + expect(wiki_body.to_s).to match(/
  • This is one line<\/li>/) + end + + it "must enrich task list checked" do + allow_any_instance_of(VimwikiMarkdown::VimwikiLink).to receive(:vimwiki_markdown_file_exists?).and_return(true) + allow(wiki_body).to receive(:get_wiki_markdown_contents).and_return('- [X] This is a checked line') + expect(wiki_body.to_s).to match(/
  • This is a checked line<\/li>/) + end + + it "must enrich task list parent 1" do + allow_any_instance_of(VimwikiMarkdown::VimwikiLink).to receive(:vimwiki_markdown_file_exists?).and_return(true) + allow(wiki_body).to receive(:get_wiki_markdown_contents).and_return('- [.] This is a parent 1 line') + expect(wiki_body.to_s).to match(/
  • This is a parent 1 line<\/li>/) + end + + it "must enrich task list parent 2" do + allow_any_instance_of(VimwikiMarkdown::VimwikiLink).to receive(:vimwiki_markdown_file_exists?).and_return(true) + allow(wiki_body).to receive(:get_wiki_markdown_contents).and_return('- [o] This is a parent 2 line') + expect(wiki_body.to_s).to match(/
  • This is a parent 2 line<\/li>/) + end + + it "must enrich task list parent 3" do + allow_any_instance_of(VimwikiMarkdown::VimwikiLink).to receive(:vimwiki_markdown_file_exists?).and_return(true) + allow(wiki_body).to receive(:get_wiki_markdown_contents).and_return('- [O] This is a parent 3 line') + expect(wiki_body.to_s).to match(/
  • This is a parent 3 line<\/li>/) + end + describe "syntax highlighting" do it "must give correct classes" do allow(wiki_body).to receive(:get_wiki_markdown_contents) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 392a7b1..84abf03 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -107,6 +107,28 @@ def wiki_index_markdown > this is a blockquote > without a linebreak + +## Test for checks + +- [ ] This is one line +- [X] This is a checked line +- [ ] This is a parent line 1 + - [ ] This is a child line 1 + - [ ] This is a child line 2 +- [.] This is a parent line 2 + - [X] This is a child line 1 + - [ ] This is a child line 2 + - [ ] This is a child line 3 +- [o] This is a parent line 2 + - [X] This is a child line 1 + - [X] This is a child line 2 + - [ ] This is a child line 3 +- [O] This is a parent line 3 + - [X] This is a child line 1 + - [X] This is a child line 2 + - [X] This is a child line 3 + - [ ] This is a child line 4 + " end