llm-docs-builder 1.1.0 → 1.1.1
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/.github/CODEOWNERS +2 -0
- data/.github/workflows/ci.yml +3 -3
- data/.github/workflows/docker.yml +2 -2
- data/.github/workflows/push.yml +2 -2
- data/.github/workflows/verify-action-pins.yml +23 -0
- data/.rubocop.yml +9 -0
- data/.ruby-version +1 -1
- data/CHANGELOG.md +29 -0
- data/Dockerfile +2 -2
- data/Gemfile.lock +21 -19
- data/README.md +20 -8
- data/lib/llm_docs_builder/bulk_transformer.rb +16 -36
- data/lib/llm_docs_builder/cli.rb +86 -78
- data/lib/llm_docs_builder/config.rb +23 -2
- data/lib/llm_docs_builder/generator.rb +8 -71
- data/lib/llm_docs_builder/markdown_finder.rb +120 -0
- data/lib/llm_docs_builder/markdown_transformer.rb +11 -10
- data/lib/llm_docs_builder/text_compressor.rb +50 -42
- data/lib/llm_docs_builder/transformers/code_blocks.rb +226 -0
- data/lib/llm_docs_builder/transformers/content_cleanup_transformer.rb +93 -25
- data/lib/llm_docs_builder/transformers/heading_transformer.rb +1 -1
- data/lib/llm_docs_builder/transformers/link_transformer.rb +27 -144
- data/lib/llm_docs_builder/version.rb +1 -1
- data/lib/llm_docs_builder.rb +21 -6
- data/llm-docs-builder.yml.example +5 -3
- data/package-lock.json +9 -34
- data/package.json +1 -1
- data/renovate.json +6 -0
- metadata +5 -1
|
@@ -12,6 +12,8 @@ module LlmDocsBuilder
|
|
|
12
12
|
#
|
|
13
13
|
# @api public
|
|
14
14
|
class TextCompressor
|
|
15
|
+
include Transformers::CodeBlocks
|
|
16
|
+
|
|
15
17
|
# Common English stopwords that can be safely removed from documentation
|
|
16
18
|
# Excludes words that might be important in technical contexts (like "not", "no")
|
|
17
19
|
STOPWORDS = %w[
|
|
@@ -35,6 +37,11 @@ module LlmDocsBuilder
|
|
|
35
37
|
throughout under until up upon within without
|
|
36
38
|
].freeze
|
|
37
39
|
|
|
40
|
+
# Word, or a code placeholder or bare URL that stopword removal leaves unchanged
|
|
41
|
+
WORD_PATTERN = %r{#{CODE_PLACEHOLDER_PATTERN}|https?://\S+|(?:(?!https?://)[[:word:]])+}
|
|
42
|
+
|
|
43
|
+
private_constant :WORD_PATTERN
|
|
44
|
+
|
|
38
45
|
# @return [Hash] compression options
|
|
39
46
|
attr_reader :options
|
|
40
47
|
|
|
@@ -57,19 +64,31 @@ module LlmDocsBuilder
|
|
|
57
64
|
# @option methods [Boolean] :remove_stopwords remove common filler words
|
|
58
65
|
# @option methods [Boolean] :remove_duplicates remove duplicate sentences/paragraphs
|
|
59
66
|
# @return [String] compressed text
|
|
67
|
+
# @note Code (fenced and indented code blocks, inline code spans) is left unchanged
|
|
60
68
|
def compress(content, methods = {})
|
|
61
|
-
|
|
69
|
+
return content.dup unless methods[:remove_stopwords] || methods[:remove_duplicates]
|
|
62
70
|
|
|
63
|
-
|
|
64
|
-
|
|
71
|
+
outside_code(content) { |text| compress_outside_code(text, methods) }
|
|
72
|
+
end
|
|
65
73
|
|
|
66
|
-
|
|
74
|
+
# Compress text whose code is already replaced by placeholders
|
|
75
|
+
#
|
|
76
|
+
# @param text [String] text with code replaced by {Transformers::CodeBlocks} placeholders
|
|
77
|
+
# @param methods [Hash] compression methods to apply
|
|
78
|
+
# @option methods [Boolean] :remove_stopwords remove common filler words
|
|
79
|
+
# @option methods [Boolean] :remove_duplicates remove duplicate sentences/paragraphs
|
|
80
|
+
# @return [String] compressed text, still with placeholders
|
|
81
|
+
# @api private
|
|
82
|
+
def compress_outside_code(text, methods = {})
|
|
83
|
+
text = remove_stopwords_outside_code(text) if methods[:remove_stopwords]
|
|
84
|
+
text = remove_duplicate_paragraphs_outside_code(text) if methods[:remove_duplicates]
|
|
85
|
+
text
|
|
67
86
|
end
|
|
68
87
|
|
|
69
88
|
# Remove stopwords from text while preserving technical content
|
|
70
89
|
#
|
|
71
90
|
# Removes common English stopwords that don't carry significant meaning.
|
|
72
|
-
# Preserves code blocks, inline code, and technical terms.
|
|
91
|
+
# Preserves code blocks, inline code, bare URLs and technical terms.
|
|
73
92
|
#
|
|
74
93
|
# @deprecated This is an aggressive optimization that may affect readability.
|
|
75
94
|
# Use with caution and test results carefully.
|
|
@@ -77,64 +96,54 @@ module LlmDocsBuilder
|
|
|
77
96
|
# @param content [String] text to process
|
|
78
97
|
# @return [String] text with stopwords removed
|
|
79
98
|
def remove_stopwords(content)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
code_counter = 0
|
|
83
|
-
|
|
84
|
-
# Extract and preserve fenced code blocks
|
|
85
|
-
content = content.gsub(/^```.*?^```/m) do |match|
|
|
86
|
-
placeholder = "___CODE_BLOCK_#{code_counter}___"
|
|
87
|
-
code_blocks[placeholder] = match
|
|
88
|
-
code_counter += 1
|
|
89
|
-
placeholder
|
|
90
|
-
end
|
|
99
|
+
outside_code(content) { |text| remove_stopwords_outside_code(text) }
|
|
100
|
+
end
|
|
91
101
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
102
|
+
# Remove duplicate paragraphs from text
|
|
103
|
+
#
|
|
104
|
+
# Detects and removes paragraphs that are the same after whitespace and case normalization.
|
|
105
|
+
# Documentation often repeats concepts across different sections. Paragraphs with code are always kept.
|
|
106
|
+
#
|
|
107
|
+
# @param content [String] text to process
|
|
108
|
+
# @return [String] text with duplicate paragraphs removed
|
|
109
|
+
def remove_duplicate_paragraphs(content)
|
|
110
|
+
outside_code(content) { |text| remove_duplicate_paragraphs_outside_code(text) }
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
private
|
|
99
114
|
|
|
115
|
+
# Remove stopwords from text whose code is replaced by placeholders
|
|
116
|
+
#
|
|
117
|
+
# @param content [String] text with code replaced by {Transformers::CodeBlocks} placeholders
|
|
118
|
+
# @return [String] text with stopwords removed
|
|
119
|
+
def remove_stopwords_outside_code(content)
|
|
100
120
|
# Get combined stopwords list
|
|
101
121
|
stopwords_list = STOPWORDS + options[:custom_stopwords]
|
|
102
122
|
|
|
103
123
|
# Process each line
|
|
104
|
-
content
|
|
124
|
+
content.split("\n").map do |line|
|
|
105
125
|
# Skip markdown headers, lists, and links
|
|
106
126
|
if line.match?(/^#+\s/) || line.match?(/^[\*\-]\s/) || line.match?(/\[[^\]]+\]\([^)]+\)/)
|
|
107
127
|
line
|
|
108
128
|
else
|
|
109
129
|
# Remove stopwords from regular text
|
|
110
|
-
|
|
111
|
-
words.map do |word|
|
|
130
|
+
line.gsub(WORD_PATTERN) do |word|
|
|
112
131
|
# Preserve the word if it's not a stopword or if we should preserve technical terms
|
|
113
132
|
if stopwords_list.include?(word.downcase) && !word.match?(/^[A-Z]/) # Don't remove capitalized words
|
|
114
133
|
''
|
|
115
134
|
else
|
|
116
135
|
word
|
|
117
136
|
end
|
|
118
|
-
end
|
|
137
|
+
end
|
|
119
138
|
end
|
|
120
139
|
end.join("\n")
|
|
121
|
-
|
|
122
|
-
# Restore code blocks
|
|
123
|
-
code_blocks.each do |placeholder, original|
|
|
124
|
-
content = content.gsub(placeholder, original)
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
content
|
|
128
140
|
end
|
|
129
141
|
|
|
130
|
-
# Remove duplicate paragraphs from text
|
|
131
|
-
#
|
|
132
|
-
# Detects and removes paragraphs that are duplicates or near-duplicates.
|
|
133
|
-
# Documentation often repeats concepts across different sections.
|
|
142
|
+
# Remove duplicate paragraphs from text whose code is replaced by placeholders
|
|
134
143
|
#
|
|
135
|
-
# @param content [String] text
|
|
144
|
+
# @param content [String] text with code replaced by {Transformers::CodeBlocks} placeholders
|
|
136
145
|
# @return [String] text with duplicate paragraphs removed
|
|
137
|
-
def
|
|
146
|
+
def remove_duplicate_paragraphs_outside_code(content)
|
|
138
147
|
# Split into paragraphs (separated by blank lines)
|
|
139
148
|
paragraphs = content.split(/\n\s*\n/)
|
|
140
149
|
|
|
@@ -149,7 +158,7 @@ module LlmDocsBuilder
|
|
|
149
158
|
# Skip empty paragraphs
|
|
150
159
|
next if normalized.empty?
|
|
151
160
|
|
|
152
|
-
#
|
|
161
|
+
# Code placeholders are unique, so a paragraph with code is never a duplicate
|
|
153
162
|
unless seen[normalized]
|
|
154
163
|
seen[normalized] = true
|
|
155
164
|
unique_paragraphs << para
|
|
@@ -158,6 +167,5 @@ module LlmDocsBuilder
|
|
|
158
167
|
|
|
159
168
|
unique_paragraphs.join("\n\n")
|
|
160
169
|
end
|
|
161
|
-
|
|
162
170
|
end
|
|
163
171
|
end
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LlmDocsBuilder
|
|
4
|
+
module Transformers
|
|
5
|
+
# Detection of code shared by transformers
|
|
6
|
+
#
|
|
7
|
+
# Finds fenced and indented code blocks and inline code spans, and runs transformations on content with the
|
|
8
|
+
# code replaced by placeholders, optionally leaving backticks inside HTML comments in place.
|
|
9
|
+
#
|
|
10
|
+
# @api private
|
|
11
|
+
module CodeBlocks
|
|
12
|
+
# Opening line of a fenced code block
|
|
13
|
+
FENCE_OPENING_PATTERN = /\A[ \t]*(`{3,}|~{3,})/
|
|
14
|
+
|
|
15
|
+
# Blockquote markers at the start of a line
|
|
16
|
+
BLOCKQUOTE_PREFIX_PATTERN = /\A(?: {0,3}> ?)*/
|
|
17
|
+
|
|
18
|
+
# Line that starts a list item
|
|
19
|
+
LIST_ITEM_PATTERN = /\A {0,3}(?:[-+*]|\d{1,9}[.)])(?:[ \t]|\s*\z)/
|
|
20
|
+
|
|
21
|
+
# ATX heading line
|
|
22
|
+
ATX_HEADING_PATTERN = /\A\#{1,6}(?:[ \t]|\s*\z)/
|
|
23
|
+
|
|
24
|
+
# Placeholder that stands in for a protected code fragment; a code block's ends with `b`
|
|
25
|
+
CODE_PLACEHOLDER_PATTERN = /\u0000(\d+)b?\u0000/
|
|
26
|
+
|
|
27
|
+
# Placeholder standing in for a protected code block, the one carrying the `b` marker
|
|
28
|
+
CODE_BLOCK_PLACEHOLDER_PATTERN = /\u0000\d+b\u0000/
|
|
29
|
+
|
|
30
|
+
# Placeholder standing in for protected inline code, the one without a marker
|
|
31
|
+
INLINE_CODE_PLACEHOLDER_PATTERN = /\u0000\d+\u0000/
|
|
32
|
+
|
|
33
|
+
# Inline code span: a backtick run closed by a run of the same length, within one paragraph
|
|
34
|
+
INLINE_CODE_PATTERN = /(?<![`\\])(`+)(?!`)(?:(?!\n[ \t]*\n)[^\u0000])+?(?<!`)\1(?!`)/m
|
|
35
|
+
|
|
36
|
+
# Start of an HTML comment or of an inline code span
|
|
37
|
+
COMMENT_OR_BACKTICK_PATTERN = /<!--|`/
|
|
38
|
+
|
|
39
|
+
# Start of an inline code span
|
|
40
|
+
BACKTICK_PATTERN = /`/
|
|
41
|
+
|
|
42
|
+
# Byte of a backtick
|
|
43
|
+
BACKTICK_BYTE = '`'.ord
|
|
44
|
+
|
|
45
|
+
# End of an HTML comment
|
|
46
|
+
COMMENT_END_PATTERN = /-->/
|
|
47
|
+
|
|
48
|
+
private_constant :FENCE_OPENING_PATTERN, :BLOCKQUOTE_PREFIX_PATTERN, :LIST_ITEM_PATTERN, :ATX_HEADING_PATTERN,
|
|
49
|
+
:CODE_PLACEHOLDER_PATTERN, :CODE_BLOCK_PLACEHOLDER_PATTERN, :INLINE_CODE_PLACEHOLDER_PATTERN,
|
|
50
|
+
:INLINE_CODE_PATTERN, :COMMENT_OR_BACKTICK_PATTERN, :BACKTICK_PATTERN, :BACKTICK_BYTE, :COMMENT_END_PATTERN
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
# Run a transformation on content with code replaced by placeholders, then restore the code
|
|
55
|
+
#
|
|
56
|
+
# @param content [String] markdown content
|
|
57
|
+
# @param block_code [Proc, nil] maps each code block before it is protected
|
|
58
|
+
# @param comments_first [Boolean, nil] leave backticks inside HTML comments in place, as a comment that starts
|
|
59
|
+
# first hides them
|
|
60
|
+
# @yieldparam text [String] content with code fragments replaced by placeholders
|
|
61
|
+
# @yieldreturn [String] transformed content
|
|
62
|
+
# @return [String] transformed content with the original code restored
|
|
63
|
+
def outside_code(content, block_code: nil, comments_first: false)
|
|
64
|
+
codes = []
|
|
65
|
+
protect = lambda do |code, block: false|
|
|
66
|
+
codes << code
|
|
67
|
+
"\u0000#{codes.size - 1}#{'b' if block}\u0000"
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
text = replace_code_blocks(content) do |code|
|
|
71
|
+
protect.call(block_code ? block_code.call(code) : code, block: true)
|
|
72
|
+
end
|
|
73
|
+
text = if comments_first
|
|
74
|
+
replace_inline_code_outside_comments(text) { |span| protect.call(span) }
|
|
75
|
+
else
|
|
76
|
+
text.gsub(INLINE_CODE_PATTERN) { protect.call(::Regexp.last_match(0)) }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
yield(text).gsub(CODE_PLACEHOLDER_PATTERN) { codes[::Regexp.last_match(1).to_i] }
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Replace inline code spans that do not start inside an HTML comment
|
|
83
|
+
#
|
|
84
|
+
# Scans once: a comment is skipped up to its first `-->`, and an unclosed comment ends the search for
|
|
85
|
+
# comments, as no later `<!--` can be closed either.
|
|
86
|
+
#
|
|
87
|
+
# @param text [String] markdown content
|
|
88
|
+
# @yieldparam span [String] inline code span
|
|
89
|
+
# @yieldreturn [String] replacement for the span
|
|
90
|
+
# @return [String] content with inline code spans replaced
|
|
91
|
+
def replace_inline_code_outside_comments(text)
|
|
92
|
+
scanner = StringScanner.new(text, fixed_anchor: true)
|
|
93
|
+
out = +''
|
|
94
|
+
copied = 0
|
|
95
|
+
start_pattern = COMMENT_OR_BACKTICK_PATTERN
|
|
96
|
+
|
|
97
|
+
while scanner.skip_until(start_pattern)
|
|
98
|
+
if text.getbyte(scanner.pos - 1) == BACKTICK_BYTE
|
|
99
|
+
start = scanner.pos -= 1
|
|
100
|
+
length = scanner.skip(INLINE_CODE_PATTERN)
|
|
101
|
+
next scanner.pos += 1 unless length
|
|
102
|
+
|
|
103
|
+
out << text.byteslice(copied, start - copied) << yield(text.byteslice(start, length))
|
|
104
|
+
copied = scanner.pos
|
|
105
|
+
elsif !scanner.skip_until(COMMENT_END_PATTERN)
|
|
106
|
+
start_pattern = BACKTICK_PATTERN
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
out << text.byteslice(copied, text.bytesize - copied)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Replace fenced and indented code blocks
|
|
114
|
+
#
|
|
115
|
+
# Indented lines count as code only after a blank line and outside lists, where they would otherwise be
|
|
116
|
+
# paragraph or list item continuations. Code inside blockquotes is detected after the `>` markers.
|
|
117
|
+
#
|
|
118
|
+
# @param content [String] markdown content
|
|
119
|
+
# @yieldparam code [String] code block lines without the final line break
|
|
120
|
+
# @yieldreturn [String] replacement for the code block
|
|
121
|
+
# @return [String] content with code blocks replaced
|
|
122
|
+
def replace_code_blocks(content, &replacement)
|
|
123
|
+
out = +''
|
|
124
|
+
code = +''
|
|
125
|
+
fence = nil
|
|
126
|
+
fence_depth = 0
|
|
127
|
+
depth = 0
|
|
128
|
+
in_list = false
|
|
129
|
+
prev_blank = true
|
|
130
|
+
|
|
131
|
+
content.each_line do |line|
|
|
132
|
+
quote = line.start_with?('>', ' ') ? line[BLOCKQUOTE_PREFIX_PATTERN] : ''
|
|
133
|
+
body = quote.empty? ? line : line.delete_prefix(quote)
|
|
134
|
+
prev_blank ||= quote.count('>') > depth
|
|
135
|
+
depth = quote.count('>')
|
|
136
|
+
fence = nil if depth < fence_depth
|
|
137
|
+
|
|
138
|
+
if fence
|
|
139
|
+
code << line
|
|
140
|
+
fence = nil if depth == fence_depth && closing_fence?(body, fence)
|
|
141
|
+
prev_blank = true
|
|
142
|
+
next
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
if !in_list && (prev_blank || !code.empty?) && indented_code?(body)
|
|
146
|
+
code << line
|
|
147
|
+
next
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
if (fence = fence_opening(body))
|
|
151
|
+
fence_depth = depth
|
|
152
|
+
in_list = in_list_after?(body, in_list, prev_blank)
|
|
153
|
+
code << line
|
|
154
|
+
next
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
out << replace_lines(code, &replacement) << line
|
|
158
|
+
code = +''
|
|
159
|
+
in_list = in_list_after?(body, in_list, prev_blank)
|
|
160
|
+
prev_blank = body.strip.empty?
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
out << replace_lines(code, &replacement)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Fence of a line that opens a fenced code block
|
|
167
|
+
#
|
|
168
|
+
# @param line [String] markdown line
|
|
169
|
+
# @return [String, nil] the opening fence, or nil when the line does not open a block
|
|
170
|
+
def fence_opening(line)
|
|
171
|
+
opening = line.match(FENCE_OPENING_PATTERN)
|
|
172
|
+
return unless opening
|
|
173
|
+
return if opening[1].start_with?('`') && opening.post_match.include?('`')
|
|
174
|
+
|
|
175
|
+
opening[1]
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Check whether a line closes a fenced code block
|
|
179
|
+
#
|
|
180
|
+
# @param line [String] markdown line without blockquote markers
|
|
181
|
+
# @param fence [String] the opening fence
|
|
182
|
+
# @return [Boolean] true when the line holds only a fence at least as long as the opening one
|
|
183
|
+
def closing_fence?(line, fence)
|
|
184
|
+
marker = line.strip
|
|
185
|
+
marker.length >= fence.length && marker.count(fence[0]) == marker.length
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# Check whether a line is indented enough to be code
|
|
189
|
+
#
|
|
190
|
+
# @param line [String] markdown line
|
|
191
|
+
# @return [Boolean] true for a non-blank line indented by four spaces or a tab
|
|
192
|
+
def indented_code?(line)
|
|
193
|
+
line.match?(/\A(?: {4}|\t)\s*\S/)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# Check whether content after a line is still inside a list
|
|
197
|
+
#
|
|
198
|
+
# A line that is not indented ends the list after a blank line, or right away when it is a heading or a fence.
|
|
199
|
+
#
|
|
200
|
+
# @param line [String] markdown line
|
|
201
|
+
# @param in_list [Boolean] whether the previous line was inside a list
|
|
202
|
+
# @param prev_blank [Boolean] whether the previous line was blank
|
|
203
|
+
# @return [Boolean] true when the list continues after this line
|
|
204
|
+
def in_list_after?(line, in_list, prev_blank)
|
|
205
|
+
return true if line.match?(LIST_ITEM_PATTERN)
|
|
206
|
+
return in_list if !in_list || line.strip.empty? || line.match?(/\A[ \t]/)
|
|
207
|
+
return in_list unless prev_blank || line.match?(ATX_HEADING_PATTERN) || fence_opening(line)
|
|
208
|
+
|
|
209
|
+
false
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# Replace collected code lines, keeping the final line break
|
|
213
|
+
#
|
|
214
|
+
# @param code [String] collected code lines
|
|
215
|
+
# @yieldparam code [String] code lines without the final line break
|
|
216
|
+
# @yieldreturn [String] replacement for the code lines
|
|
217
|
+
# @return [String] replacement, or an empty string when there is no code
|
|
218
|
+
def replace_lines(code)
|
|
219
|
+
return '' if code.empty?
|
|
220
|
+
|
|
221
|
+
body = code.chomp
|
|
222
|
+
"#{yield(body)}#{code.delete_prefix(body)}"
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
end
|
|
@@ -10,6 +10,7 @@ module LlmDocsBuilder
|
|
|
10
10
|
# @api public
|
|
11
11
|
class ContentCleanupTransformer
|
|
12
12
|
include BaseTransformer
|
|
13
|
+
include CodeBlocks
|
|
13
14
|
|
|
14
15
|
# Transform content by removing unwanted elements
|
|
15
16
|
#
|
|
@@ -21,22 +22,90 @@ module LlmDocsBuilder
|
|
|
21
22
|
# @option options [Boolean] :remove_code_examples remove code blocks
|
|
22
23
|
# @option options [Boolean] :remove_images remove image syntax
|
|
23
24
|
# @option options [Boolean] :remove_blockquotes remove blockquote formatting
|
|
25
|
+
# @yieldparam text [String] cleaned content with code replaced by placeholders, so that a later step can
|
|
26
|
+
# skip code without detecting it again
|
|
27
|
+
# @yieldreturn [String] content to restore the code into
|
|
24
28
|
# @return [String] transformed content
|
|
25
|
-
|
|
29
|
+
# @note Comments, badges, images and blockquote markers inside code are left unchanged
|
|
30
|
+
def transform(content, options = {}, &)
|
|
26
31
|
result = content.dup
|
|
27
32
|
|
|
28
33
|
result = remove_frontmatter(result) if options[:remove_frontmatter]
|
|
29
|
-
result
|
|
30
|
-
|
|
31
|
-
result = remove_code_examples(result
|
|
34
|
+
return clean_outside_code(result, options, &) unless options[:remove_code_examples]
|
|
35
|
+
|
|
36
|
+
result = remove_code_examples(result, options)
|
|
32
37
|
result = remove_images(result) if options[:remove_images]
|
|
33
38
|
result = remove_blockquotes(result) if options[:remove_blockquotes]
|
|
34
|
-
|
|
35
|
-
result
|
|
39
|
+
block_given? ? yield(result) : result
|
|
36
40
|
end
|
|
37
41
|
|
|
38
42
|
private
|
|
39
43
|
|
|
44
|
+
# Code block lines inside a blockquote start with this marker
|
|
45
|
+
QUOTED_CODE_MARKER_PATTERN = /^> ?/
|
|
46
|
+
|
|
47
|
+
# HTML comment
|
|
48
|
+
COMMENT_PATTERN = /<!--.*?-->/m
|
|
49
|
+
|
|
50
|
+
# Image alt text; it may hold inline code but never crosses a code block placeholder
|
|
51
|
+
ALT_TEXT_PATTERN = /(?:[^\]\u0000]|#{INLINE_CODE_PLACEHOLDER_PATTERN})*/
|
|
52
|
+
|
|
53
|
+
# Inline image; its destination never crosses a code placeholder
|
|
54
|
+
IMAGE_PATTERN = /!\[#{ALT_TEXT_PATTERN}\]\([^)\u0000]+\)/
|
|
55
|
+
|
|
56
|
+
# Reference-style image
|
|
57
|
+
REFERENCE_IMAGE_PATTERN = /!\[#{ALT_TEXT_PATTERN}\]\[[^\]\u0000]+\]/
|
|
58
|
+
|
|
59
|
+
# Badge image
|
|
60
|
+
BADGE_PATTERN =
|
|
61
|
+
/!\[#{ALT_TEXT_PATTERN}\]\([^)\u0000]*(?:badge|shield|svg|travis|coveralls|fury)[^)\u0000]*\)/i
|
|
62
|
+
|
|
63
|
+
# Badge image wrapped in a link
|
|
64
|
+
LINKED_BADGE_PATTERN = /\[#{BADGE_PATTERN}\]\([^)\u0000]*\)/
|
|
65
|
+
|
|
66
|
+
# Text that each removal outside code can change
|
|
67
|
+
REMOVABLE_PATTERNS = {
|
|
68
|
+
remove_comments: /<!--/,
|
|
69
|
+
remove_badges: BADGE_PATTERN,
|
|
70
|
+
remove_images: /!\[/,
|
|
71
|
+
remove_blockquotes: /^>/
|
|
72
|
+
}.freeze
|
|
73
|
+
|
|
74
|
+
private_constant :QUOTED_CODE_MARKER_PATTERN, :COMMENT_PATTERN, :ALT_TEXT_PATTERN, :IMAGE_PATTERN,
|
|
75
|
+
:REFERENCE_IMAGE_PATTERN, :BADGE_PATTERN, :LINKED_BADGE_PATTERN, :REMOVABLE_PATTERNS
|
|
76
|
+
|
|
77
|
+
# Run removals on content outside code
|
|
78
|
+
#
|
|
79
|
+
# With blockquote removal, code blocks inside a blockquote lose one level of quote markers, like the text
|
|
80
|
+
# around them.
|
|
81
|
+
#
|
|
82
|
+
# @param content [String] markdown content
|
|
83
|
+
# @param options [Hash] removal options
|
|
84
|
+
# @option options [Boolean] :remove_comments remove HTML comments
|
|
85
|
+
# @option options [Boolean] :remove_badges remove badge images
|
|
86
|
+
# @option options [Boolean] :remove_images remove image syntax
|
|
87
|
+
# @option options [Boolean] :remove_blockquotes remove blockquote formatting
|
|
88
|
+
# @yieldparam text [String] cleaned content with code replaced by placeholders
|
|
89
|
+
# @yieldreturn [String] content to restore the code into
|
|
90
|
+
# @return [String] cleaned content
|
|
91
|
+
# @note Without a block, code is detected only when a removal can change the content
|
|
92
|
+
def clean_outside_code(content, options)
|
|
93
|
+
removals = REMOVABLE_PATTERNS.select { |option, pattern| options[option] && content.match?(pattern) }
|
|
94
|
+
return content if removals.empty? && !block_given?
|
|
95
|
+
|
|
96
|
+
if removals.key?(:remove_blockquotes)
|
|
97
|
+
unquote = ->(code) { code.start_with?('>') ? code.gsub(QUOTED_CODE_MARKER_PATTERN, '') : code }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
outside_code(content, block_code: unquote, comments_first: removals.key?(:remove_comments)) do |text|
|
|
101
|
+
text = remove_comments(text) if removals.key?(:remove_comments)
|
|
102
|
+
text = remove_badges(text) if removals.key?(:remove_badges)
|
|
103
|
+
text = remove_images(text) if removals.key?(:remove_images)
|
|
104
|
+
text = remove_blockquotes(text) if removals.key?(:remove_blockquotes)
|
|
105
|
+
block_given? ? yield(text) : text
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
40
109
|
# Remove YAML or TOML frontmatter
|
|
41
110
|
#
|
|
42
111
|
# @param content [String] markdown content
|
|
@@ -52,7 +121,7 @@ module LlmDocsBuilder
|
|
|
52
121
|
# @param content [String] markdown content
|
|
53
122
|
# @return [String] content without comments
|
|
54
123
|
def remove_comments(content)
|
|
55
|
-
content.gsub(
|
|
124
|
+
content.gsub(COMMENT_PATTERN, '')
|
|
56
125
|
end
|
|
57
126
|
|
|
58
127
|
# Remove badge images
|
|
@@ -60,26 +129,29 @@ module LlmDocsBuilder
|
|
|
60
129
|
# @param content [String] markdown content
|
|
61
130
|
# @return [String] content without badges
|
|
62
131
|
def remove_badges(content)
|
|
63
|
-
|
|
64
|
-
content = content.gsub(/\[\!\[([^\]]*)\]\([^\)]*(?:badge|shield|svg|travis|coveralls|fury)[^\)]*\)\]\([^\)]*\)/i, '')
|
|
65
|
-
# Remove standalone badges
|
|
66
|
-
content = content.gsub(/!\[([^\]]*)\]\([^\)]*(?:badge|shield|svg|travis|coveralls|fury)[^\)]*\)/i, '')
|
|
67
|
-
content
|
|
132
|
+
content.gsub(LINKED_BADGE_PATTERN, '').gsub(BADGE_PATTERN, '')
|
|
68
133
|
end
|
|
69
134
|
|
|
70
135
|
# Remove code blocks and inline code
|
|
71
136
|
#
|
|
137
|
+
# Comments and badges are removed in the same code scan, before the code, so that code holding a comment
|
|
138
|
+
# marker does not end or start a comment.
|
|
139
|
+
#
|
|
72
140
|
# @param content [String] markdown content
|
|
141
|
+
# @param options [Hash] removal options
|
|
142
|
+
# @option options [Boolean] :remove_comments remove HTML comments
|
|
143
|
+
# @option options [Boolean] :remove_badges remove badge images
|
|
73
144
|
# @return [String] content without code
|
|
74
|
-
def remove_code_examples(content)
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
content = content
|
|
78
|
-
|
|
79
|
-
|
|
145
|
+
def remove_code_examples(content, options)
|
|
146
|
+
removed = ->(_code) { '' }
|
|
147
|
+
|
|
148
|
+
content = outside_code(content, block_code: removed, comments_first: options[:remove_comments]) do |text|
|
|
149
|
+
text = remove_comments(text) if options[:remove_comments]
|
|
150
|
+
text = remove_badges(text) if options[:remove_badges]
|
|
151
|
+
text
|
|
152
|
+
end
|
|
80
153
|
# Remove inline code
|
|
81
|
-
content
|
|
82
|
-
content
|
|
154
|
+
content.gsub(/`[^`]+`/, '')
|
|
83
155
|
end
|
|
84
156
|
|
|
85
157
|
# Remove image syntax
|
|
@@ -87,11 +159,7 @@ module LlmDocsBuilder
|
|
|
87
159
|
# @param content [String] markdown content
|
|
88
160
|
# @return [String] content without images
|
|
89
161
|
def remove_images(content)
|
|
90
|
-
|
|
91
|
-
content = content.gsub(/!\[([^\]]*)\]\([^\)]+\)/, '')
|
|
92
|
-
# Remove reference-style images
|
|
93
|
-
content = content.gsub(/!\[([^\]]*)\]\[[^\]]+\]/, '')
|
|
94
|
-
content
|
|
162
|
+
content.gsub(IMAGE_PATTERN, '').gsub(REFERENCE_IMAGE_PATTERN, '')
|
|
95
163
|
end
|
|
96
164
|
|
|
97
165
|
# Remove blockquote formatting
|
|
@@ -55,7 +55,7 @@ module LlmDocsBuilder
|
|
|
55
55
|
|
|
56
56
|
if heading_match && heading_match[1].length.between?(1, 6)
|
|
57
57
|
level = heading_match[1].length
|
|
58
|
-
title = heading_match[2]
|
|
58
|
+
title = heading_match[2]
|
|
59
59
|
|
|
60
60
|
# Update heading stack to current level
|
|
61
61
|
heading_stack = heading_stack[0...level - 1]
|