Skip to content

Commit a61f206

Browse files
Merge pull request #2 from segphault/replace_monkey_patching
Replace monkey patching
2 parents ce63bde + 094c28e commit a61f206

30 files changed

+123
-134
lines changed

lib/motion-markdown-it/common/simpleidn.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
# encoding: UTF-8
22
# Borrowed from https://github.com/mmriis/simpleidn
33
#------------------------------------------------------------------------------
4-
class Integer
5-
def to_utf8_character
6-
[self].pack("U*")
7-
end
8-
end
94

105
module SimpleIDN
116

@@ -21,7 +16,11 @@ module Punycode
2116
SKEW = 38
2217
MAXINT = 0x7FFFFFFF
2318

24-
module_function
19+
module_function
20+
21+
def to_utf8_character(int)
22+
[int].pack("U*")
23+
end
2524

2625
# decode_digit(cp) returns the numeric value of a basic code
2726
# point (for use in representing integers) in the range 0 to
@@ -75,7 +74,7 @@ def decode(input)
7574
# Handle the basic code points: Let basic be the number of input code
7675
# points before the last delimiter, or 0 if there is none, then
7776
# copy the first basic code points to the output.
78-
basic = input.rindex(DELIMITER.to_utf8_character) || 0
77+
basic = input.rindex(to_utf8_character(DELIMITER)) || 0
7978

8079
input.unpack("U*")[0, basic].each do |char|
8180
raise(RangeError, "Illegal input >= 0x80") if char >= 0x80
@@ -126,7 +125,7 @@ def decode(input)
126125
i %= out
127126

128127
# Insert n at position i of the output:
129-
output.insert(i, n.to_utf8_character)
128+
output.insert(i, to_utf8_character(n))
130129
i += 1
131130
end
132131

@@ -202,7 +201,7 @@ def encode(input)
202201
delta += 1
203202
n += 1
204203
end
205-
return output.collect {|c| c.to_utf8_character}.join
204+
return output.collect {|c| to_utf8_character(c)}.join
206205
end
207206

208207
end

lib/motion-markdown-it/common/string.rb

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/motion-markdown-it/common/utils.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ def fromCodePoint(c)
5555
def fromCharCode(c)
5656
c.chr
5757
end
58-
58+
59+
#------------------------------------------------------------------------------
60+
def charCodeAt(str, ch)
61+
str[ch].ord unless str[ch].nil?
62+
end
5963

6064
UNESCAPE_MD_RE = /\\([\!\"\#\$\%\&\'\(\)\*\+\,\-.\/:;<=>?@\[\\\]^_`{|}~])/
6165

@@ -70,8 +74,8 @@ def replaceEntityPattern(match, name)
7074

7175
return fromCodePoint(MarkdownIt::HTMLEntities::MAPPINGS[name]) if MarkdownIt::HTMLEntities::MAPPINGS[name]
7276

73-
if (name.charCodeAt(0) == 0x23 && DIGITAL_ENTITY_TEST_RE =~ name) # '#'
74-
code = name[1].downcase == 'x' ? name.slice_to_end(2).to_i(16) : name.slice_to_end(1).to_i
77+
if (charCodeAt(name, 0) == 0x23 && DIGITAL_ENTITY_TEST_RE =~ name) # '#'
78+
code = name[1].downcase == 'x' ? name[2..-1].to_i(16) : name[1..-1].to_i
7579
if (isValidEntityCode(code))
7680
return fromCodePoint(code)
7781
end

lib/motion-markdown-it/helpers/parse_link_destination.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ def parseLinkDestination(str, pos, max)
1111
start = pos
1212
result = {ok: false, pos: 0, lines: 0, str: ''}
1313

14-
if (str.charCodeAt(pos) == 0x3C ) # <
14+
if (charCodeAt(str, pos) == 0x3C ) # <
1515
pos += 1
1616
while (pos < max)
17-
code = str.charCodeAt(pos)
17+
code = charCodeAt(str, pos)
1818
return result if (code == 0x0A || isSpace(code)) # \n
1919
if (code == 0x3E) # >
2020
result[:pos] = pos + 1
@@ -38,7 +38,7 @@ def parseLinkDestination(str, pos, max)
3838

3939
level = 0
4040
while (pos < max)
41-
code = str.charCodeAt(pos)
41+
code = charCodeAt(str, pos)
4242

4343
break if (code == 0x20)
4444

lib/motion-markdown-it/helpers/parse_link_label.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def parseLinkLabel(state, start, disableNested = false)
1515
level = 1
1616

1717
while (state.pos < max)
18-
marker = state.src.charCodeAt(state.pos)
18+
marker = charCodeAt(state.src, state.pos)
1919
if (marker == 0x5D) # ]
2020
level -= 1
2121
if (level == 0)

lib/motion-markdown-it/helpers/parse_link_title.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def parseLinkTitle(str, pos, max)
1212

1313
return result if (pos >= max)
1414

15-
marker = str.charCodeAt(pos)
15+
marker = charCodeAt(str, pos)
1616

1717
return result if (marker != 0x22 && marker != 0x27 && marker != 0x28) # " ' (
1818

@@ -22,7 +22,7 @@ def parseLinkTitle(str, pos, max)
2222
marker = 0x29 if (marker == 0x28)
2323

2424
while (pos < max)
25-
code = str.charCodeAt(pos)
25+
code = charCodeAt(str, pos)
2626
if (code == marker)
2727
result[:pos] = pos + 1
2828
result[:lines] = lines
@@ -33,7 +33,7 @@ def parseLinkTitle(str, pos, max)
3333
lines += 1
3434
elsif (code == 0x5C && pos + 1 < max) # \
3535
pos += 1
36-
if (str.charCodeAt(pos) == 0x0A)
36+
if (charCodeAt(str, pos) == 0x0A)
3737
lines += 1
3838
end
3939
end

lib/motion-markdown-it/rules_block/blockquote.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def self.blockquote(state, startLine, endLine, silent)
1515
return false if (state.sCount[startLine] - state.blkIndent >= 4)
1616

1717
# check the block quote marker
18-
return false if state.src.charCodeAt(pos) != 0x3E # >
18+
return false if charCodeAt(state.src, pos) != 0x3E # >
1919
pos += 1
2020

2121
# we know that it's going to be a valid blockquote,
@@ -26,15 +26,15 @@ def self.blockquote(state, startLine, endLine, silent)
2626
initial = offset = state.sCount[startLine] + pos - (state.bMarks[startLine] + state.tShift[startLine])
2727

2828
# skip one optional space after '>'
29-
if state.src.charCodeAt(pos) == 0x20 # space
29+
if charCodeAt(state.src, pos) == 0x20 # space
3030
# ' > test '
3131
# ^ -- position start of line here:
3232
pos += 1
3333
initial += 1
3434
offset +=1
3535
adjustTab = false
3636
spaceAfterMarker = true
37-
elsif state.src.charCodeAt(pos) == 0x09 # tab
37+
elsif charCodeAt(state.src, pos) == 0x09 # tab
3838
spaceAfterMarker = true
3939

4040
if ((state.bsCount[startLine] + offset) % 4 == 3)
@@ -58,7 +58,7 @@ def self.blockquote(state, startLine, endLine, silent)
5858
state.bMarks[startLine] = pos
5959

6060
while pos < max
61-
ch = state.src.charCodeAt(pos)
61+
ch = charCodeAt(state.src, pos)
6262

6363
if isSpace(ch)
6464
if ch == 0x09
@@ -128,23 +128,23 @@ def self.blockquote(state, startLine, endLine, silent)
128128
break
129129
end
130130

131-
if state.src.charCodeAt(pos) == 0x3E && !wasOutdented # >
131+
if charCodeAt(state.src, pos) == 0x3E && !wasOutdented # >
132132
pos += 1
133133
# This line is inside the blockquote.
134134

135135
# skip spaces after ">" and re-calculate offset
136136
initial = offset = state.sCount[nextLine] + pos - (state.bMarks[nextLine] + state.tShift[nextLine])
137137

138138
# skip one optional space after '>'
139-
if state.src.charCodeAt(pos) == 0x20 # space
139+
if charCodeAt(state.src, pos) == 0x20 # space
140140
# ' > test '
141141
# ^ -- position start of line here:
142142
pos += 1
143143
initial += 1
144144
offset += 1
145145
adjustTab = false
146146
spaceAfterMarker = true
147-
elsif state.src.charCodeAt(pos) == 0x09 # tab
147+
elsif charCodeAt(state.src, pos) == 0x09 # tab
148148
spaceAfterMarker = true
149149

150150
if ((state.bsCount[nextLine] + offset) % 4 == 3)
@@ -168,7 +168,7 @@ def self.blockquote(state, startLine, endLine, silent)
168168
state.bMarks[nextLine] = pos
169169

170170
while pos < max
171-
ch = state.src.charCodeAt(pos)
171+
ch = charCodeAt(state.src, pos)
172172

173173
if isSpace(ch)
174174
if ch == 0x09

lib/motion-markdown-it/rules_block/fence.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def self.fence(state, startLine, endLine, silent)
1616

1717
return false if pos + 3 > max
1818

19-
marker = state.src.charCodeAt(pos)
19+
marker = charCodeAt(state.src, pos)
2020

2121
if marker != 0x7E && marker != 0x60 # != ~ && != `
2222
return false
@@ -58,7 +58,7 @@ def self.fence(state, startLine, endLine, silent)
5858
break
5959
end
6060

61-
next if state.src.charCodeAt(pos) != marker
61+
next if charCodeAt(state.src, pos) != marker
6262

6363
if state.sCount[nextLine] - state.blkIndent >= 4
6464
# closing fence should be indented less than 4 spaces

lib/motion-markdown-it/rules_block/heading.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ def self.heading(state, startLine, endLine, silent)
1313
# if it's indented more than 3 spaces, it should be a code block
1414
return false if state.sCount[startLine] - state.blkIndent >= 4
1515

16-
ch = state.src.charCodeAt(pos)
16+
ch = charCodeAt(state.src, pos)
1717

1818
return false if (ch != 0x23 || pos >= max)
1919

2020
# count heading level
2121
level = 1
2222
pos += 1
23-
ch = state.src.charCodeAt(pos)
23+
ch = charCodeAt(state.src, pos)
2424
while (ch == 0x23 && pos < max && level <= 6) # '#'
2525
level += 1
2626
pos += 1
27-
ch = state.src.charCodeAt(pos)
27+
ch = charCodeAt(state.src, pos)
2828
end
2929

3030
return false if (level > 6 || (pos < max && !isSpace(ch)))
@@ -35,7 +35,7 @@ def self.heading(state, startLine, endLine, silent)
3535

3636
max = state.skipSpacesBack(max, pos)
3737
tmp = state.skipCharsBack(max, 0x23, pos) # '#'
38-
if (tmp > pos && isSpace(state.src.charCodeAt(tmp - 1)))
38+
if (tmp > pos && isSpace(charCodeAt(state.src, tmp - 1)))
3939
max = tmp
4040
end
4141

lib/motion-markdown-it/rules_block/hr.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def self.hr(state, startLine, endLine, silent)
1313
# if it's indented more than 3 spaces, it should be a code block
1414
return false if (state.sCount[startLine] - state.blkIndent >= 4)
1515

16-
marker = state.src.charCodeAt(pos)
16+
marker = charCodeAt(state.src, pos)
1717
pos += 1
1818

1919
# Check hr marker
@@ -27,7 +27,7 @@ def self.hr(state, startLine, endLine, silent)
2727

2828
cnt = 1
2929
while (pos < max)
30-
ch = state.src.charCodeAt(pos)
30+
ch = charCodeAt(state.src, pos)
3131
pos += 1
3232
return false if ch != marker && !isSpace(ch)
3333
cnt += 1 if ch == marker

0 commit comments

Comments
 (0)