immosquare-cleaner 0.1.115 → 0.1.116
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/immosquare-cleaner/markdown.rb +70 -7
- data/lib/immosquare-cleaner/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d3bb81cf1641c678335ba6485d0457824ad4f2efd9bec1ad6a3e7973a83617e7
|
|
4
|
+
data.tar.gz: b66e76e63984cd595a981fae0a84eb82d4bd47a64092b1373878223da757889b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d00cc397871e5131c6cdb3edc8ef9ed40422677c98b9903ce1a2844273f28993c6c679ba8032ed27edfa06b2387d48794fdf5b1dfecc67729336c2a36d29ef82
|
|
7
|
+
data.tar.gz: 4e5097e2d7b6db8a2d94ef4e45d6513a065c4d2e7da8be077e2ec2acc3f3588d9a39df20e27162bd3d5e84a49f71c5cf551d638ddc3ba102a02ec5dc8644b231
|
|
@@ -17,16 +17,60 @@ module ImmosquareCleaner
|
|
|
17
17
|
##============================================================##
|
|
18
18
|
FENCE_REGEX = /\A\s*(```|~~~)/
|
|
19
19
|
|
|
20
|
+
##============================================================##
|
|
21
|
+
## A list item marker is `*`, `+` or `-` followed by a space,
|
|
22
|
+
## or alone on its line. The space is what makes it a list:
|
|
23
|
+
## without that requirement `---` — a thematic break, and the
|
|
24
|
+
## fence of a YAML frontmatter — and `*emphasis*` opening a
|
|
25
|
+
## line are both read as list items, which injects a blank
|
|
26
|
+
## line right after them. Inside a frontmatter that blank line
|
|
27
|
+
## lands between the opening fence and the first key.
|
|
28
|
+
##============================================================##
|
|
29
|
+
LIST_ITEM_REGEX = /\A[*+-](\s|\z)/
|
|
30
|
+
|
|
31
|
+
##============================================================##
|
|
32
|
+
## The fence of a YAML frontmatter: `---` alone on its line.
|
|
33
|
+
##============================================================##
|
|
34
|
+
FRONTMATTER_FENCE_REGEX = /\A---\s*\z/
|
|
35
|
+
|
|
20
36
|
def clean(file_path)
|
|
21
|
-
results
|
|
22
|
-
array_to_parse
|
|
23
|
-
lines
|
|
24
|
-
fence_marker
|
|
37
|
+
results = []
|
|
38
|
+
array_to_parse = []
|
|
39
|
+
lines = []
|
|
40
|
+
fence_marker = nil
|
|
41
|
+
frontmatter_last = frontmatter_last_index(file_path)
|
|
42
|
+
frontmatter_opened = false
|
|
25
43
|
|
|
26
44
|
##============================================================##
|
|
27
45
|
## We parse each line of the file
|
|
28
46
|
##============================================================##
|
|
29
|
-
File.foreach(file_path) do |current_line|
|
|
47
|
+
File.foreach(file_path).with_index do |current_line, index|
|
|
48
|
+
##============================================================##
|
|
49
|
+
## A frontmatter is YAML, not markdown: it goes through
|
|
50
|
+
## verbatim. Neither the list spacing nor the table rules
|
|
51
|
+
## mean anything between its fences, and applying them adds
|
|
52
|
+
## a blank line after the opening fence — or before the
|
|
53
|
+
## closing one when the last key holds a list.
|
|
54
|
+
##
|
|
55
|
+
## Its leading blank lines are the one exception, and they
|
|
56
|
+
## are dropped: never meaningful in YAML, and earlier
|
|
57
|
+
## versions of this cleaner injected one there by taking the
|
|
58
|
+
## opening `---` for a list item. Removing it here repairs
|
|
59
|
+
## the files that already carry it.
|
|
60
|
+
##============================================================##
|
|
61
|
+
if frontmatter_last && index <= frontmatter_last
|
|
62
|
+
lines << current_line
|
|
63
|
+
|
|
64
|
+
if index > 0 && index < frontmatter_last && !frontmatter_opened
|
|
65
|
+
next if current_line.strip.empty?
|
|
66
|
+
|
|
67
|
+
frontmatter_opened = true
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
results << current_line
|
|
71
|
+
next
|
|
72
|
+
end
|
|
73
|
+
|
|
30
74
|
##============================================================##
|
|
31
75
|
## We save the last line to know if we need to add a newline
|
|
32
76
|
##============================================================##
|
|
@@ -81,6 +125,25 @@ module ImmosquareCleaner
|
|
|
81
125
|
|
|
82
126
|
private
|
|
83
127
|
|
|
128
|
+
##============================================================##
|
|
129
|
+
## Index of the closing fence of the frontmatter, or nil when
|
|
130
|
+
## the file has none. A frontmatter only exists when `---` is
|
|
131
|
+
## the very first line AND a closing `---` follows: without
|
|
132
|
+
## that second condition, a file opening on a thematic break
|
|
133
|
+
## would be emitted verbatim from end to end.
|
|
134
|
+
##============================================================##
|
|
135
|
+
def frontmatter_last_index(file_path)
|
|
136
|
+
first_line = File.foreach(file_path).first
|
|
137
|
+
return nil if first_line.nil? || !first_line.match?(FRONTMATTER_FENCE_REGEX)
|
|
138
|
+
|
|
139
|
+
File.foreach(file_path).with_index do |line, index|
|
|
140
|
+
next if index == 0
|
|
141
|
+
return index if line.match?(FRONTMATTER_FENCE_REGEX)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
nil
|
|
145
|
+
end
|
|
146
|
+
|
|
84
147
|
##============================================================##
|
|
85
148
|
## we want to clean the markdown files to have a uniform style
|
|
86
149
|
## for the tables.
|
|
@@ -156,8 +219,8 @@ module ImmosquareCleaner
|
|
|
156
219
|
|
|
157
220
|
cleaned_previous = previous_line.rstrip
|
|
158
221
|
blank_line = current_line.gsub("\n", "").empty?
|
|
159
|
-
previous_is_list = cleaned_previous.lstrip.
|
|
160
|
-
current_is_list = cleaned_current.lstrip.
|
|
222
|
+
previous_is_list = cleaned_previous.lstrip.match?(LIST_ITEM_REGEX)
|
|
223
|
+
current_is_list = cleaned_current.lstrip.match?(LIST_ITEM_REGEX)
|
|
161
224
|
final = previous_is_list && !current_is_list && !blank_line ? ["\n"] : []
|
|
162
225
|
final << ["#{cleaned_current}\n"]
|
|
163
226
|
end
|