Module: Markita::Markdown::Inline

Defined in:
lib/markita/markdown/inline.rb

Overview

Module to isolate from Markdown :reek:TooManyConstants

Constant Summary collapse

ENTITY =

When the writer does not mean to invoke a substitutions caused by a special character, that character can be escaped with a backslash. This is then replaced with its HTML entity.

/\\([<>*"~`_&;:\\])/
FOOTNOTE =
/\[\^(\d+)\](:)?/
SUBSCRIPT =
/\\\(([^()]+)\)/
SUPERSCRIPT =
/\\\^\(([^()]+)\)/
URL =
%r{(https?://[\w./&+?%-]+)}
BOLD =
/\*([^*]+)\*/
CODE =
/`([^`]+)`/
EMOJI =
/:(\w+):/
EMOJIS =
Hash[*File.read(PATH['emojis.tsv']).split(/\s+/)]
ITALIC =
/"([^"]+)"/
STRIKE =
/~([^~]+)~/
UNDERLINE =
/_([^_]+)_/
ANCHOR =
/\[([^\[\]]+)\]\(([^(")]+)\)/

Class Method Summary collapse

Class Method Details

.anchor(mdt) ⇒ Object



58
59
60
61
62
63
# File 'lib/markita/markdown/inline.rb', line 58

def self.anchor(mdt)
  href, title = mdt[2].split(/\s+/, 2).map(&:strip)
  title = %( title="#{title}") if title
  text = tag(mdt[1], EMOJI, method(:emoji))
  %(<a href="#{href}"#{title}>#{text}</a>)
end

.bold(mdt) ⇒ Object



39
# File 'lib/markita/markdown/inline.rb', line 39

def self.bold(mdt) = "<b>#{mdt[1]}</b>"

.code(mdt) ⇒ Object



42
# File 'lib/markita/markdown/inline.rb', line 42

def self.code(mdt) = "<code>#{mdt[1].gsub('<', '&lt;')}</code>"

.emoji(mdt) ⇒ Object



46
# File 'lib/markita/markdown/inline.rb', line 46

def self.emoji(mdt) = (emj = EMOJIS[mdt[1]]) ? "&#x#{emj};" : mdt[0]

.entity(mdt) ⇒ Object



17
# File 'lib/markita/markdown/inline.rb', line 17

def self.entity(mdt) = "&##{mdt[1].ord};"

.footnote(mdt = nil, num: mdt[1], ref: mdt[2]) ⇒ Object

:reek:ControlParameter



21
22
23
24
25
26
27
# File 'lib/markita/markdown/inline.rb', line 21

def self.footnote(mdt = nil, num: mdt[1], ref: mdt[2])
  if ref
    %(<a id="fn:#{num}" href="#fnref:#{num}">#{num}:</a>)
  else
    %(<a id="fnref:#{num}" href="#fn:#{num}"><sup>#{num}</sup></a>)
  end
end

.italic(mdt) ⇒ Object



49
# File 'lib/markita/markdown/inline.rb', line 49

def self.italic(mdt) = "<i>#{mdt[1]}</i>"

.strike(mdt) ⇒ Object



52
# File 'lib/markita/markdown/inline.rb', line 52

def self.strike(mdt) = "<s>#{mdt[1]}</s>"

.subscript(mdt) ⇒ Object



30
# File 'lib/markita/markdown/inline.rb', line 30

def self.subscript(mdt) = "<sub>#{mdt[1]}</sub>"

.superscript(mdt) ⇒ Object



33
# File 'lib/markita/markdown/inline.rb', line 33

def self.superscript(mdt) = "<sup>#{mdt[1]}</sup>"

.tag(entry, regx, m2string, &block) ⇒ Object

:reek:DuplicateMethodCall :reek:TooManyStatements rubocop:disable Metrics/MethodLength



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/markita/markdown/inline.rb', line 69

def self.tag(entry, regx, m2string, &block)
  if (mdt = regx.match entry)
    string = String.new
    while mdt
      pre_match = (block ? block.call(mdt.pre_match) : mdt.pre_match)
      string << (pre_match + m2string[mdt])
      post_match = mdt.post_match
      mdt = regx.match(post_match)
    end
    string << (block ? block.call(post_match) : post_match)
    return string
  end
  block ? block.call(entry) : entry
end

.tags(line) ⇒ Object

:reek:NestedIterators :reek:TooManyStatements :reek:UncommunicativeVariableName rubocop:disable Metrics/AbcSize



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/markita/markdown/inline.rb', line 87

def self.tags(line)
  line = tag(line, ENTITY, method(:entity))
  line = tag(line, CODE, method(:code)) do |string|
    tag(string, ANCHOR, method(:anchor)) do |str|
      tag(str, URL, method(:url)) do |s|
        s = tag(s, EMOJI,       method(:emoji))
        s = tag(s, BOLD,        method(:bold))
        s = tag(s, ITALIC,      method(:italic))
        s = tag(s, STRIKE,      method(:strike))
        s = tag(s, UNDERLINE,   method(:underline))
        s = tag(s, FOOTNOTE,    method(:footnote))
        s = tag(s, SUPERSCRIPT, method(:superscript))
        tag(s, SUBSCRIPT, method(:subscript))
      end
    end
  end
  line.sub(/ ?[ \\]$/, '<br>')
end

.underline(mdt) ⇒ Object



55
# File 'lib/markita/markdown/inline.rb', line 55

def self.underline(mdt) = "<u>#{mdt[1]}</u>"

.url(mdt, hrf = mdt[1]) ⇒ Object



36
# File 'lib/markita/markdown/inline.rb', line 36

def self.url(mdt, hrf = mdt[1]) = %(<a href="#{hrf}">#{hrf}</a>)