Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Commonmarker v1 api #10

Merged
merged 9 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .ci.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ platform :ruby do
gem 'rinku' # dependency for wikicloth for handling links

gem 'RedCloth'
gem 'commonmarker', '< 1'

if ENV['LATEST_COMMONMARKER'] == 'yes'
jeremyevans marked this conversation as resolved.
Show resolved Hide resolved
gem 'commonmarker'
else
gem 'commonmarker', '< 1'
end

gem 'rdiscount', '>= 2.1.6'
gem 'redcarpet'
gem 'yajl-ruby'
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ jobs:
fail-fast: false
matrix:
ruby: [ "2.0.0", 2.1, 2.3, 2.4, 2.5, 2.6, 2.7, "3.0", 3.1, 3.2, 3.3, jruby-9.1, jruby-9.2, jruby-9.3, jruby-9.4 ]
latest_commonmarker: ["", "yes"]
jeremyevans marked this conversation as resolved.
Show resolved Hide resolved
include:
- ruby: "3.1"
latest_commonmarker: "yes"
- ruby: "3.2"
latest_commonmarker: "yes"
- ruby: "3.3"
latest_commonmarker: "yes"
name: ${{ matrix.ruby }}
env:
BUNDLE_GEMFILE: .ci.gemfile
COFFEE_SCRIPT: use
LATEST_COMMONMARKER: ${{ matrix.latest_commonmarker }}
steps:
- uses: actions/checkout@v4
# WikiCloth needs IDN
Expand Down
138 changes: 93 additions & 45 deletions lib/tilt/commonmarker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,101 @@
require_relative 'template'
require 'commonmarker'

aliases = {
:smartypants => :SMART
}.freeze
parse_opts = [
:FOOTNOTES,
:LIBERAL_HTML_TAG,
:SMART,
:smartypants,
:STRIKETHROUGH_DOUBLE_TILDE,
:UNSAFE,
:VALIDATE_UTF8,
].freeze
render_opts = [
:FOOTNOTES,
:FULL_INFO_STRING,
:GITHUB_PRE_LANG,
:HARDBREAKS,
:NOBREAKS,
:SAFE, # Removed in v0.18.0 (2018-10-17)
:SOURCEPOS,
:TABLE_PREFER_STYLE_ATTRIBUTES,
:UNSAFE,
].freeze
exts = [
:autolink,
:strikethrough,
:table,
:tagfilter,
:tasklist,
].freeze


Tilt::CommonMarkerTemplate = Tilt::StaticTemplate.subclass do
extensions = exts.select do |extension|
@options[extension]
end
module Tilt
class CommonMarkerTemplate < StaticTemplate
ALIASES = {
:smartypants => :SMART
}.freeze
PARSE_OPTS = [
:FOOTNOTES,
:LIBERAL_HTML_TAG,
:SMART,
:smartypants,
:STRIKETHROUGH_DOUBLE_TILDE,
:UNSAFE,
:VALIDATE_UTF8,
].freeze
RENDER_OPTS = [
:FOOTNOTES,
:FULL_INFO_STRING,
:GITHUB_PRE_LANG,
:HARDBREAKS,
:NOBREAKS,
:SAFE, # Removed in v0.18.0 (2018-10-17)
:SOURCEPOS,
:TABLE_PREFER_STYLE_ATTRIBUTES,
:UNSAFE,
].freeze
EXTS = [
:autolink,
:strikethrough,
:table,
:tagfilter,
:tasklist,
].freeze

V1_PARSE_OPTS = [
jeremyevans marked this conversation as resolved.
Show resolved Hide resolved
:smart,
:default_info_string,
].freeze
V1_RENDER_OPTS = [
:hardbreaks,
:github_pre_lang,
:width,
:unsafe,
:escape,
:sourcepos,
].freeze
V1_EXTS = [
:strikethrough,
:tagfilter,
:table,
:autolink,
:tasklist,
:superscript,
:header_ids,
:footnotes,
:description_lists,
:front_matter_delimiter,
:shortcodes,
].freeze

parse_options, render_options = [parse_opts, render_opts].map do |opts|
opts = opts.select do |option|
@options[option]
end.map! do |option|
aliases[option] || option
def prepare
if commonmaker_v1_or_later?
prepare_v1
else
prepare_pre
end
end

opts = :DEFAULT unless opts.any?
opts
end
private def prepare_pre
extensions = EXTS.select do |extension|
@options[extension]
end

CommonMarker.render_doc(@data, parse_options, extensions).to_html(render_options, extensions)
parse_options, render_options = [PARSE_OPTS, RENDER_OPTS].map do |opts|
opts = opts.select do |option|
@options[option]
end.map! do |option|
ALIASES[option] || option
end

opts = :DEFAULT unless opts.any?
opts
end

@output = CommonMarker.render_doc(@data, parse_options, extensions).to_html(render_options, extensions)
end

private def prepare_v1
parse_options = @options.select { |key, _| V1_PARSE_OPTS.include?(key) }
render_options = @options.select { |key, _| V1_RENDER_OPTS.include?(key) }
extensions = @options.select { |key, _| V1_EXTS.include?(key) }
@output = Commonmarker.to_html(@data, options: { parse: parse_options, render: render_options, extension: extensions })
end

private def commonmaker_v1_or_later?
defined?(::Commonmarker) ? true : false
end
end
end
91 changes: 70 additions & 21 deletions test/tilt_commonmarkertemplate_test.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,96 @@
require_relative 'test_helper'

checked_describe 'tilt/commonmarker' do
it "preparing and evaluating templates on #render" do
template = Tilt::CommonMarkerTemplate.new { |t| "# Hello World!" }
assert_equal "<h1>Hello World!</h1>\n", template.render
end
if ENV['LATEST_COMMONMARKER'] == 'yes'
jeremyevans marked this conversation as resolved.
Show resolved Hide resolved
it "preparing and evaluating templates on #render" do
template = Tilt::CommonMarkerTemplate.new { |t| "# Hello World!" }
assert_equal "<h1>Hello World!</h1>\n", template.render
end

it "can be rendered more than once" do
template = Tilt::CommonMarkerTemplate.new { |t| "# Hello World!" }
3.times { assert_equal "<h1>Hello World!</h1>\n", template.render }
end
it "can be rendered more than once" do
template = Tilt::CommonMarkerTemplate.new { |t| "# Hello World!" }
3.times { assert_equal "<h1>Hello World!</h1>\n", template.render }
end

it "smartypants when :smartypants is set" do
template = Tilt::CommonMarkerTemplate.new(:smartypants => true) do |t|
"OKAY -- 'Smarty Pants'"
it "smartypants when :smatr is set" do
template = Tilt::CommonMarkerTemplate.new(smart: true) do |t|
"OKAY -- 'Smarty Pants'"
end
assert_match('<p>OKAY – ‘Smarty Pants’</p>', template.render)
end
assert_match('<p>OKAY – ‘Smarty Pants’</p>', template.render)
end

it 'Renders unsafe HTML when :UNSAFE is set' do
template = Tilt::CommonMarkerTemplate.new(UNSAFE: true) do |_t|
<<MARKDOWN
it 'Renders unsafe HTML when :unsafe is set' do
template = Tilt::CommonMarkerTemplate.new(unsafe: true) do |_t|
<<MARKDOWN
<div class="alert alert-info full-width">
<h5 class="card-title">TL;DR</h5>
<p>This is an unsafe HTML block</p>
</div>

And then some **other** Markdown
MARKDOWN
end

expected = <<EXPECTED_HTML
<div class="alert alert-info full-width">
<h5 class="card-title">TL;DR</h5>
<p>This is an unsafe HTML block</p>
</div>
<p>And then some <strong>other</strong> Markdown</p>
EXPECTED_HTML

assert_match(expected, template.render)
end

it "autolinking when :autolink is set" do
template = Tilt::CommonMarkerTemplate.new(autolink: true) do |t|
"https://example.com"
end
assert_match('<a href="https://example.com">https://example.com</a>', template.render)
end
else
it "preparing and evaluating templates on #render" do
template = Tilt::CommonMarkerTemplate.new { |t| "# Hello World!" }
assert_equal "<h1>Hello World!</h1>\n", template.render
end

expected = <<EXPECTED_HTML
it "can be rendered more than once" do
template = Tilt::CommonMarkerTemplate.new { |t| "# Hello World!" }
3.times { assert_equal "<h1>Hello World!</h1>\n", template.render }
end

it "smartypants when :smartypants is set" do
template = Tilt::CommonMarkerTemplate.new(:smartypants => true) do |t|
"OKAY -- 'Smarty Pants'"
end
assert_match('<p>OKAY – ‘Smarty Pants’</p>', template.render)
end

it 'Renders unsafe HTML when :UNSAFE is set' do
template = Tilt::CommonMarkerTemplate.new(UNSAFE: true) do |_t|
<<MARKDOWN
<div class="alert alert-info full-width">
<h5 class="card-title">TL;DR</h5>
<p>This is an unsafe HTML block</p>
</div>

And then some **other** Markdown
MARKDOWN
end

expected = <<EXPECTED_HTML
<div class="alert alert-info full-width">
<h5 class="card-title">TL;DR</h5>
<p>This is an unsafe HTML block</p>
</div>
<p>And then some <strong>other</strong> Markdown</p>
EXPECTED_HTML

assert_match(expected, template.render)
end
assert_match(expected, template.render)
end

it "sets allows_script metadata set to false" do
assert_equal false, Tilt::CommonMarkerTemplate.new { |t| "# Hello World!" }.metadata[:allows_script]
it "sets allows_script metadata set to false" do
assert_equal false, Tilt::CommonMarkerTemplate.new { |t| "# Hello World!" }.metadata[:allows_script]
end
end
end
Loading