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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 19779dc732954ca8de22b3c3fa9ed64570df1c5962ddd692d3eaead3795bcb33
4
- data.tar.gz: 8482c46bb1437146f369e6a9e3995f29e9e8e240b08f54adabf2bc22211bf74f
3
+ metadata.gz: d3bb81cf1641c678335ba6485d0457824ad4f2efd9bec1ad6a3e7973a83617e7
4
+ data.tar.gz: b66e76e63984cd595a981fae0a84eb82d4bd47a64092b1373878223da757889b
5
5
  SHA512:
6
- metadata.gz: c6651cf24dc96341e01b8e7d94977dbb476725f9d86b2aa7a75f815d36d14ccc2b923511b28aae3d9634e5579d454d630739efab8c7caf4bfb8c490afb6f7dcc
7
- data.tar.gz: 8f10906affbec34cf93fc92c26af2cf8cf9f9c10e48b7aedfc364fdb5b96a2931f365a503963aa7aa91a8edc6e0b34e0e91e74b022a7488b6719ad3e9ddb2888
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 = nil
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.start_with?("*", "-", "+")
160
- current_is_list = cleaned_current.lstrip.start_with?("*", "-", "+")
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
@@ -1,3 +1,3 @@
1
1
  module ImmosquareCleaner
2
- VERSION = "0.1.115".freeze
2
+ VERSION = "0.1.116".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-cleaner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.115
4
+ version: 0.1.116
5
5
  platform: ruby
6
6
  authors:
7
7
  - immosquare