Skip to content

Commit faf916b

Browse files
Clean up Markd::Options
Add documentation for options Don't stick multiple vars in the same property instantiation Enable `Naming/QueryBoolMethods` and fix all infractions Don't require `gfm` option for tagfilter and autolink extensions
1 parent 80d7754 commit faf916b

11 files changed

Lines changed: 90 additions & 59 deletions

File tree

.ameba.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ Metrics/CyclomaticComplexity:
88
- src/markd/parsers/block.cr
99
- src/markd/renderer.cr
1010

11-
Naming/QueryBoolMethods:
12-
Enabled: false
13-
1411
Naming/BlockParameterName:
1512
Enabled: false
1613

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Markd.to_html(markdown, options)
5656
| safe | `Bool` | false | if **true**, raw HTML will not be passed through to HTML output (it will be replaced by comments) |
5757
| prettyprint | `Bool` | false | if **true**, code tags generated by code blocks will have a `prettyprint` class added to them, to be used by [Google code-prettify](https://github.com/google/code-prettify). |
5858
| gfm | `Bool` | false | **Partial support** |
59-
| autolink | `Bool` | false | if **true**, more autolinks are detected, like bare email addresses or http links. Requires `gfm` be set to `true` |
59+
| autolink | `Bool` | false | if **true**, more autolinks are detected, like bare email addresses or http links |
6060
| toc | `Bool` | false | **Not supported for now** |
6161
| base_url | `URI?` | nil | if not **nil**, relative URLs of links are resolved against this `URI`. It act's like HTML's `<base href="base_url">` in the context of a Markdown document. |
6262

src/markd/options.cr

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ require "uri"
33
module Markd
44
# Markdown rendering options.
55
class Options
6-
property time, gfm, toc
6+
# Render parsing cost time for reading the source, parsing blocks, and parsing inline.
7+
property? time : Bool
8+
9+
# Enables GitHub Flavored Markdown support.
10+
#
11+
# https://github.github.com/gfm/
12+
property? gfm : Bool
13+
14+
# Not supported for now.
15+
property? toc : Bool
716

817
# If `true`:
918
# - straight quotes will be made curly
@@ -12,41 +21,58 @@ module Markd
1221
# - `...` will be changed to ellipses
1322
property? smart : Bool
1423

15-
@[Deprecated("Use `#smart?` instead.")]
16-
getter smart
17-
1824
# If `true`, source position information for block-level elements
1925
# will be rendered in the `data-sourcepos` attribute (for HTML).
2026
property? source_pos : Bool
2127

22-
@[Deprecated("Use `#source_pos?` instead.")]
23-
getter source_pos
24-
2528
# If `true`, raw HTML will not be passed through to HTML output
2629
# (it will be replaced by comments).
2730
property? safe : Bool
2831

29-
@[Deprecated("Use `#safe?` instead.")]
30-
getter safe
31-
3232
# If `true`, code tags generated by code blocks will have a
3333
# prettyprint class added to them, to be used by
3434
# [Google code-prettify](https://github.com/google/code-prettify).
3535
property? prettyprint : Bool
3636

37-
@[Deprecated("Use `#prettyprint?` instead.")]
38-
getter prettyprint
39-
4037
# If `base_url` is not `nil`, it is used to resolve URLs of relative
4138
# links. It act's like HTML's `<base href="base_url">` in the context
4239
# of a Markdown document.
4340
property base_url : URI?
4441

45-
property emoji : Bool
42+
# Enables GFM emoji support.
43+
#
44+
# For example:
45+
#
46+
# ```
47+
# @octocat :+1: This PR looks great - it's ready to merge! :ship:
48+
# ```
49+
#
50+
# Becomes:
51+
#
52+
# ```
53+
# @octocat 👍 This PR looks great - it's ready to merge! 🚢
54+
# ```
55+
# https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#using-emojis
56+
property? emoji : Bool
4657

47-
property tagfilter : Bool
58+
# If `true`, the following HTML tags will be filtered when rendering HTML output:
59+
#
60+
# * `<title>`
61+
# * `<textarea>`
62+
# * `<style>`
63+
# * `<xmp>`
64+
# * `<iframe>`
65+
# * `<noembed>`
66+
# * `<noframes>`
67+
# * `<script>`
68+
# * `<plaintext>`
69+
#
70+
# All other HTML tags are left untouched.
71+
property? tagfilter : Bool
4872

49-
property autolink : Bool
73+
# If `true`, more autolinks will be detected.
74+
# Setting to `false` does not disable autolink support as a whole.
75+
property? autolink : Bool
5076

5177
def initialize(
5278
@time = false,
@@ -62,5 +88,25 @@ module Markd
6288
@base_url = nil,
6389
)
6490
end
91+
92+
# Deprecated
93+
94+
@[Deprecated("Use `#time?` instead.")]
95+
getter time
96+
97+
@[Deprecated("Use `#gfm?` instead.")]
98+
getter gfm
99+
100+
@[Deprecated("Use `#smart?` instead.")]
101+
getter smart
102+
103+
@[Deprecated("Use `#source_pos?` instead.")]
104+
getter source_pos
105+
106+
@[Deprecated("Use `#safe?` instead.")]
107+
getter safe
108+
109+
@[Deprecated("Use `#prettyprint?` instead.")]
110+
getter prettyprint
65111
end
66112
end

src/markd/parsers/block.cr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module Markd::Parser
2626
getter line, current_line, blank, inline_lexer,
2727
indent, indented, next_nonspace, refmap
2828

29-
delegate gfm, tagfilter, to: @options
29+
delegate gfm?, tagfilter?, to: @options
3030

3131
def initialize(@options : Options)
3232
@inline_lexer = Inline.new(@options)
@@ -55,11 +55,11 @@ module Markd::Parser
5555
end
5656

5757
def parse(source : String)
58-
Utils.timer("block parsing", @options.time) do
58+
Utils.timer("block parsing", @options.time?) do
5959
parse_blocks(source)
6060
end
6161

62-
Utils.timer("inline parsing", @options.time) do
62+
Utils.timer("inline parsing", @options.time?) do
6363
process_inlines
6464
end
6565

src/markd/parsers/inline.cr

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module Markd::Parser
4343
when '*', '_'
4444
handle_delim(char, node)
4545
when '~'
46-
if @options.gfm
46+
if @options.gfm?
4747
handle_delim(char, node)
4848
else
4949
string(node)
@@ -61,15 +61,15 @@ module Markd::Parser
6161
when 'w'
6262
# Catch www. autolinks for GFM
6363
# Do not match if it's http://www
64-
if @options.gfm && @options.autolink && (@pos == 0 || char_at?(@pos - 1) != '/')
64+
if @options.autolink? && (@pos == 0 || char_at?(@pos - 1) != '/')
6565
auto_link(node)
6666
else
6767
false
6868
end
6969
when 'h'
7070
# Catch http:// and https:// autolinks for GFM
7171
# Do not match if it's <http:// ... because that was matched by '<'
72-
if @options.gfm && @options.autolink && (
72+
if @options.autolink? && (
7373
@pos == 0 ||
7474
# Do not match if it's <http:// ... because that was matched by '<'
7575
char_at?(@pos - 1) != '<' ||
@@ -83,21 +83,21 @@ module Markd::Parser
8383
when 'f'
8484
# Catch ftp:// autolinks for GFM
8585
# Do not match if it's <ftp:// ... because that was matched by '<'
86-
if @options.gfm && @options.autolink && (@pos == 0 || char_at?(@pos - 1) != '<')
86+
if @options.autolink? && (@pos == 0 || char_at?(@pos - 1) != '<')
8787
auto_link(node)
8888
else
8989
false
9090
end
9191
when 'x'
9292
# Catch xmpp: autolinks for GFM
93-
if @options.gfm && @options.autolink && (@pos == 0 || char_at?(@pos - 1) != '<')
93+
if @options.autolink? && (@pos == 0 || char_at?(@pos - 1) != '<')
9494
auto_link(node)
9595
else
9696
false
9797
end
9898
when 'm'
9999
# Catch mailto: autolinks for GFM
100-
if @options.gfm && @options.autolink && (@pos == 0 || char_at?(@pos - 1) != '<')
100+
if @options.autolink? && (@pos == 0 || char_at?(@pos - 1) != '<')
101101
auto_link(node)
102102
else
103103
false
@@ -107,7 +107,7 @@ module Markd::Parser
107107
when ':'
108108
emoji(node)
109109
else
110-
if @options.gfm && @options.autolink && node.text.includes? '@'
110+
if @options.autolink? && node.text.includes? '@'
111111
# Catch email autolinks for GFM
112112
auto_link(node)
113113
else
@@ -356,7 +356,7 @@ module Markd::Parser
356356
'"' => delimiter,
357357
} of Char => Delimiter?
358358

359-
openers_bottom['~'] = delimiter if @options.gfm
359+
openers_bottom['~'] = delimiter if @options.gfm?
360360

361361
# move forward, looking for closers, and handling each
362362
while closer
@@ -386,7 +386,7 @@ module Markd::Parser
386386

387387
case closer_char
388388
when '*', '_', '~'
389-
if closer_char != '~' || (closer_char == '~' && @options.gfm)
389+
if closer_char != '~' || (closer_char == '~' && @options.gfm?)
390390
if opener
391391
# calculate actual number of delimiters used from closer
392392
use_delims = (closer.num_delims >= 2 && opener.num_delims >= 2) ? 2 : 1
@@ -458,8 +458,6 @@ module Markd::Parser
458458
opener.node.text = "\u{201C}"
459459
end
460460
closer = closer.next?
461-
else
462-
nil
463461
end
464462

465463
if !opener && !odd_match
@@ -482,7 +480,7 @@ module Markd::Parser
482480
elsif (matched_text = match(Rule::AUTO_LINK))
483481
node.append_child(link(matched_text, false))
484482
return true
485-
elsif @options.gfm && @options.autolink
483+
elsif @options.autolink?
486484
# These are all the extended autolinks from the
487485
# autolink extension
488486

@@ -535,7 +533,7 @@ module Markd::Parser
535533
if (text = match(Rule::HTML_TAG))
536534
child = Node.new(Node::Type::HTMLInline)
537535

538-
if @options.gfm && @options.tagfilter
536+
if @options.tagfilter?
539537
text = Rule::HTMLBlock.escape_disallowed_html(text)
540538
end
541539

@@ -562,8 +560,6 @@ module Markd::Parser
562560
break
563561
when Char::ZERO, nil
564562
return false
565-
else
566-
nil
567563
end
568564
end
569565
text = @text.byte_slice((@pos + 1), (pos - 1) - (@pos + 1))
@@ -579,7 +575,7 @@ module Markd::Parser
579575
end
580576

581577
private def emoji(node : Node)
582-
return false unless @options.emoji
578+
return false unless @options.emoji?
583579

584580
if char_at?(@pos) == ':'
585581
pos = @pos + 1
@@ -932,7 +928,7 @@ module Markd::Parser
932928
#
933929
# If we are at the beginning of the string, then we return
934930
# the chunk matched
935-
if @options.gfm && @options.autolink
931+
if @options.autolink?
936932
advance = special_string?(@text, @pos)
937933
if advance > 0
938934
if @pos > start_pos
@@ -1047,7 +1043,7 @@ module Markd::Parser
10471043
when '\n', '`', '[', ']', '\\', '!', '<', '&', '*', '_', '\'', '"', ':', 'w'
10481044
false
10491045
when '~'
1050-
!@options.gfm
1046+
!@options.gfm?
10511047
else
10521048
true
10531049
end
@@ -1076,8 +1072,6 @@ module Markd::Parser
10761072
if byte_index < @text.bytesize
10771073
reader = Char::Reader.new(@text, byte_index)
10781074
reader.current_char
1079-
else
1080-
nil
10811075
end
10821076
end
10831077

src/markd/renderer.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ module Markd
7373
abstract def table_cell(node : Node, entering : Bool) : Nil
7474

7575
def render(document : Node, formatter : T?) forall T
76-
Utils.timer("rendering", @options.time) do
76+
Utils.timer("rendering", @options.time?) do
7777
walker = document.walker
7878
while (event = walker.next)
7979
node, entering = event

src/markd/renderers/html_renderer.cr

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Markd
1414
if entering
1515
newline
1616
tag(tag_name, attrs(node))
17-
toc(node) if @options.toc
17+
toc(node) if @options.toc?
1818
else
1919
tag(tag_name, end_tag: true)
2020
newline
@@ -281,11 +281,11 @@ module Markd
281281
end
282282

283283
def strong(node : Node, entering : Bool) : Nil
284-
@strong_stack -= 1 if @options.gfm && !entering
284+
@strong_stack -= 1 if @options.gfm? && !entering
285285

286286
tag("strong", end_tag: !entering) if @strong_stack == 0
287287

288-
@strong_stack += 1 if @options.gfm && entering
288+
@strong_stack += 1 if @options.gfm? && entering
289289
end
290290

291291
def strikethrough(node : Node, entering : Bool) : Nil
@@ -337,8 +337,6 @@ module Markd
337337
private def attrs(node : Node)
338338
if @options.source_pos? && (pos = node.source_pos)
339339
{"data-source-pos" => "#{pos[0][0]}:#{pos[0][1]}-#{pos[1][0]}:#{pos[1][1]}"}
340-
else
341-
nil
342340
end
343341
end
344342

@@ -356,8 +354,6 @@ module Markd
356354
code_tag_attrs = attrs(node)
357355
pre_tag_attrs = if @options.prettyprint?
358356
{"class" => "prettyprint"}
359-
else
360-
nil
361357
end
362358

363359
tag("pre", pre_tag_attrs) do
@@ -375,8 +371,6 @@ module Markd
375371
code_tag_attrs = attrs(node)
376372
pre_tag_attrs = if @options.prettyprint?
377373
{"class" => "prettyprint"}
378-
else
379-
nil
380374
end
381375

382376
lang = code_block_language(languages)

src/markd/rules/block_quote.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Markd::Rule
66
if match?(parser)
77
seek(parser)
88
parser.close_unmatched_blocks
9-
if parser.gfm && (match = parser.line.match(Rule::ADMONITION_START))
9+
if parser.gfm? && (match = parser.line.match(Rule::ADMONITION_START))
1010
node = parser.add_child(Node::Type::Alert, parser.next_nonspace)
1111
# This is an alert
1212
node.data["alert"] = match[1]

src/markd/rules/html_block.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module Markd::Rule
3131
def token(parser : Parser, container : Node) : Nil
3232
text = container.text.gsub(/(\n *)+$/, "")
3333

34-
if parser.gfm && parser.tagfilter
34+
if parser.tagfilter?
3535
text = self.class.escape_disallowed_html(text)
3636
end
3737

0 commit comments

Comments
 (0)