llm-docs-builder 1.0.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 +10 -4
- data/.github/workflows/docker.yml +5 -5
- 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 +59 -6
- data/Dockerfile +2 -2
- data/Gemfile.lock +27 -25
- data/README.md +41 -17
- data/lib/llm_docs_builder/bulk_transformer.rb +16 -31
- 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 +78 -72
- 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/output_formatter.rb +1 -1
- 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 +200 -29
- data/lib/llm_docs_builder/version.rb +1 -1
- data/lib/llm_docs_builder.rb +22 -6
- data/llm-docs-builder.yml.example +7 -5
- data/package-lock.json +15 -40
- data/package.json +1 -1
- data/renovate.json +6 -0
- metadata +6 -2
data/lib/llm_docs_builder/cli.rb
CHANGED
|
@@ -46,21 +46,39 @@ module LlmDocsBuilder
|
|
|
46
46
|
when 'version'
|
|
47
47
|
show_version
|
|
48
48
|
else
|
|
49
|
-
|
|
50
|
-
puts "Run 'llm-docs-builder --help' for usage information"
|
|
51
|
-
exit 1
|
|
49
|
+
exit_with_usage_error("Unknown command: #{options[:command]}")
|
|
52
50
|
end
|
|
51
|
+
rescue OptionParser::ParseError => e
|
|
52
|
+
exit_with_usage_error("Error: #{e.message}")
|
|
53
53
|
rescue LlmDocsBuilder::Errors::BaseError => e
|
|
54
|
-
|
|
55
|
-
exit 1
|
|
54
|
+
exit_with_error("Error: #{e.message}")
|
|
56
55
|
rescue StandardError => e
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
lines = ["Unexpected error: #{e.message}"]
|
|
57
|
+
lines << e.backtrace.join("\n") if options&.fetch(:verbose, false)
|
|
58
|
+
exit_with_error(*lines)
|
|
60
59
|
end
|
|
61
60
|
|
|
62
61
|
private
|
|
63
62
|
|
|
63
|
+
# Print error lines to stderr and exit
|
|
64
|
+
#
|
|
65
|
+
# Uses $stderr directly because Kernel#warn prints nothing when warnings are disabled (-W0).
|
|
66
|
+
#
|
|
67
|
+
# @param lines [Array<String>] lines to print
|
|
68
|
+
# @raise [SystemExit] always, with status 1
|
|
69
|
+
def exit_with_error(*lines)
|
|
70
|
+
lines.each { |line| $stderr.puts(line) }
|
|
71
|
+
exit 1
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Print a usage error with a pointer to the help and exit
|
|
75
|
+
#
|
|
76
|
+
# @param message [String] error message
|
|
77
|
+
# @raise [SystemExit] always, with status 1
|
|
78
|
+
def exit_with_usage_error(message)
|
|
79
|
+
exit_with_error(message, "Run 'llm-docs-builder --help' for usage information")
|
|
80
|
+
end
|
|
81
|
+
|
|
64
82
|
# Parse command-line options using OptionParser
|
|
65
83
|
#
|
|
66
84
|
# Extracts command and options from argv. First non-flag argument is treated as command name.
|
|
@@ -97,7 +115,7 @@ module LlmDocsBuilder
|
|
|
97
115
|
options[:docs] = path
|
|
98
116
|
end
|
|
99
117
|
|
|
100
|
-
opts.on('-o', '--output PATH', 'Output file path') do |path|
|
|
118
|
+
opts.on('-o', '--output PATH', 'Output file path (transform prints to stdout without it)') do |path|
|
|
101
119
|
options[:output] = path
|
|
102
120
|
end
|
|
103
121
|
|
|
@@ -139,15 +157,12 @@ module LlmDocsBuilder
|
|
|
139
157
|
# @raise [SystemExit] exits with status 1 if docs path not found
|
|
140
158
|
def generate(options)
|
|
141
159
|
# Load config and merge with CLI options
|
|
142
|
-
config =
|
|
160
|
+
config = load_config(options[:config])
|
|
143
161
|
merged_options = config.merge_with_options(options)
|
|
144
162
|
|
|
145
163
|
docs_path = merged_options[:docs]
|
|
146
164
|
|
|
147
|
-
unless File.exist?(docs_path)
|
|
148
|
-
puts "Documentation path not found: #{docs_path}"
|
|
149
|
-
exit 1
|
|
150
|
-
end
|
|
165
|
+
exit_with_error("Documentation path not found: #{docs_path}") unless File.exist?(docs_path)
|
|
151
166
|
|
|
152
167
|
puts "Generating llms.txt from #{docs_path}..." if merged_options[:verbose]
|
|
153
168
|
|
|
@@ -175,14 +190,15 @@ module LlmDocsBuilder
|
|
|
175
190
|
# @param options [Hash] command options from parse_options
|
|
176
191
|
# @option options [String] :config path to config file
|
|
177
192
|
# @option options [String] :docs path to markdown file (required)
|
|
178
|
-
# @option options [String] :output output file path
|
|
193
|
+
# @option options [String] :output output file path (prints to stdout when not given;
|
|
194
|
+
# the config's output is not used)
|
|
179
195
|
# @option options [String] :base_url base URL for link expansion
|
|
180
196
|
# @option options [Boolean] :convert_urls convert .html to .md
|
|
181
197
|
# @option options [Boolean] :verbose enable verbose output
|
|
182
198
|
# @raise [SystemExit] exits with status 1 if file not found or -d flag missing
|
|
183
199
|
def transform(options)
|
|
184
200
|
# Load config and merge with CLI options
|
|
185
|
-
config =
|
|
201
|
+
config = load_config(options[:config])
|
|
186
202
|
merged_options = config.merge_with_options(options)
|
|
187
203
|
|
|
188
204
|
url = options[:url]
|
|
@@ -190,16 +206,10 @@ module LlmDocsBuilder
|
|
|
190
206
|
config_file_path = config['docs']
|
|
191
207
|
file_path = url ? cli_file_path : (cli_file_path || config_file_path)
|
|
192
208
|
|
|
193
|
-
if url && cli_file_path
|
|
194
|
-
puts 'Cannot use both --docs and --url for transform command'
|
|
195
|
-
exit 1
|
|
196
|
-
end
|
|
209
|
+
exit_with_error('Cannot use both --docs and --url for transform command') if url && cli_file_path
|
|
197
210
|
|
|
198
211
|
unless file_path
|
|
199
|
-
unless url
|
|
200
|
-
puts 'File path required for transform command (use -d/--docs)'
|
|
201
|
-
exit 1
|
|
202
|
-
end
|
|
212
|
+
exit_with_error('File path required for transform command (use -d/--docs)') unless url
|
|
203
213
|
end
|
|
204
214
|
|
|
205
215
|
content =
|
|
@@ -211,10 +221,7 @@ module LlmDocsBuilder
|
|
|
211
221
|
transform_options = merged_options.merge(content: remote_content, docs: nil, source_url: url)
|
|
212
222
|
LlmDocsBuilder.transform_markdown(nil, transform_options)
|
|
213
223
|
else
|
|
214
|
-
unless File.exist?(file_path)
|
|
215
|
-
puts "File not found: #{file_path}"
|
|
216
|
-
exit 1
|
|
217
|
-
end
|
|
224
|
+
exit_with_error("File not found: #{file_path}") unless File.exist?(file_path)
|
|
218
225
|
|
|
219
226
|
puts "Transforming #{file_path}..." if merged_options[:verbose]
|
|
220
227
|
|
|
@@ -222,9 +229,10 @@ module LlmDocsBuilder
|
|
|
222
229
|
LlmDocsBuilder.transform_markdown(file_path, merged_options)
|
|
223
230
|
end
|
|
224
231
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
232
|
+
# The config's output names the generate target, so only -o writes a file here
|
|
233
|
+
if options[:output]
|
|
234
|
+
File.write(options[:output], content.sub(/\n?\z/, "\n"))
|
|
235
|
+
puts "Transformed content saved to #{options[:output]}"
|
|
228
236
|
else
|
|
229
237
|
puts content
|
|
230
238
|
end
|
|
@@ -245,19 +253,15 @@ module LlmDocsBuilder
|
|
|
245
253
|
# @raise [SystemExit] exits with status 1 if directory not found or transformation fails
|
|
246
254
|
def bulk_transform(options)
|
|
247
255
|
# Load config and merge with CLI options
|
|
248
|
-
config =
|
|
256
|
+
config = load_config(options[:config])
|
|
249
257
|
merged_options = config.merge_with_options(options)
|
|
250
258
|
|
|
251
259
|
docs_path = merged_options[:docs]
|
|
252
260
|
|
|
253
|
-
unless File.exist?(docs_path)
|
|
254
|
-
puts "Documentation path not found: #{docs_path}"
|
|
255
|
-
exit 1
|
|
256
|
-
end
|
|
261
|
+
exit_with_error("Documentation path not found: #{docs_path}") unless File.exist?(docs_path)
|
|
257
262
|
|
|
258
263
|
unless File.directory?(docs_path)
|
|
259
|
-
|
|
260
|
-
exit 1
|
|
264
|
+
exit_with_error("Path must be a directory for bulk transformation: #{docs_path}")
|
|
261
265
|
end
|
|
262
266
|
|
|
263
267
|
puts "Bulk transforming markdown files in #{docs_path}..." if merged_options[:verbose]
|
|
@@ -281,8 +285,7 @@ module LlmDocsBuilder
|
|
|
281
285
|
end
|
|
282
286
|
end
|
|
283
287
|
rescue LlmDocsBuilder::Errors::BaseError => e
|
|
284
|
-
|
|
285
|
-
exit 1
|
|
288
|
+
exit_with_error("Error during bulk transformation: #{e.message}")
|
|
286
289
|
end
|
|
287
290
|
end
|
|
288
291
|
|
|
@@ -290,32 +293,31 @@ module LlmDocsBuilder
|
|
|
290
293
|
#
|
|
291
294
|
# Reads and parses llms.txt file, displaying title, description, and links.
|
|
292
295
|
# Defaults to 'llms.txt' in current directory if no file specified.
|
|
296
|
+
# The config file's docs setting is the docs directory for generate, so it is not used here.
|
|
293
297
|
#
|
|
294
298
|
# @param options [Hash] command options from parse_options
|
|
295
|
-
# @option options [String] :config path to config file
|
|
296
299
|
# @option options [String] :docs path to llms.txt file (defaults to 'llms.txt')
|
|
297
|
-
# @option options [Boolean] :verbose
|
|
300
|
+
# @option options [Boolean] :verbose also list each link under its section count
|
|
298
301
|
# @raise [SystemExit] exits with status 1 if file not found
|
|
299
302
|
def parse(options)
|
|
300
|
-
|
|
301
|
-
config = LlmDocsBuilder::Config.new(options[:config])
|
|
302
|
-
merged_options = config.merge_with_options(options)
|
|
303
|
+
file_path = options[:docs] || 'llms.txt'
|
|
303
304
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
unless File.exist?(file_path)
|
|
307
|
-
puts "File not found: #{file_path}"
|
|
308
|
-
exit 1
|
|
309
|
-
end
|
|
305
|
+
exit_with_error("File not found: #{file_path}") unless File.exist?(file_path)
|
|
310
306
|
|
|
311
307
|
parsed = LlmDocsBuilder.parse(file_path)
|
|
312
308
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
309
|
+
puts "Title: #{parsed.title}"
|
|
310
|
+
puts "Description: #{parsed.description}"
|
|
311
|
+
|
|
312
|
+
{
|
|
313
|
+
'Documentation' => parsed.documentation_links,
|
|
314
|
+
'Example' => parsed.example_links,
|
|
315
|
+
'Optional' => parsed.optional_links
|
|
316
|
+
}.each do |section, links|
|
|
317
|
+
puts "#{section} Links: #{links.size}"
|
|
318
|
+
next unless options[:verbose]
|
|
319
|
+
|
|
320
|
+
links.each { |link| puts " - #{link[:title]}: #{link[:url]}" }
|
|
319
321
|
end
|
|
320
322
|
end
|
|
321
323
|
|
|
@@ -334,15 +336,16 @@ module LlmDocsBuilder
|
|
|
334
336
|
url = options[:url]
|
|
335
337
|
|
|
336
338
|
unless url
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
339
|
+
exit_with_error(
|
|
340
|
+
'URL required for compare command (use -u/--url)',
|
|
341
|
+
'',
|
|
342
|
+
'Examples:',
|
|
343
|
+
' # Compare remote versions (different User-Agents)',
|
|
344
|
+
' llm-docs-builder compare --url https://example.com/docs/page.html',
|
|
345
|
+
'',
|
|
346
|
+
' # Compare remote with local file',
|
|
347
|
+
' llm-docs-builder compare --url https://example.com/docs/page.html --file docs/page.md'
|
|
348
|
+
)
|
|
346
349
|
end
|
|
347
350
|
|
|
348
351
|
comparator_options = {
|
|
@@ -356,8 +359,7 @@ module LlmDocsBuilder
|
|
|
356
359
|
result = comparator.compare
|
|
357
360
|
OutputFormatter.display_comparison_results(result)
|
|
358
361
|
rescue LlmDocsBuilder::Errors::BaseError => e
|
|
359
|
-
|
|
360
|
-
exit 1
|
|
362
|
+
exit_with_error("Error during comparison: #{e.message}")
|
|
361
363
|
end
|
|
362
364
|
end
|
|
363
365
|
|
|
@@ -365,22 +367,15 @@ module LlmDocsBuilder
|
|
|
365
367
|
#
|
|
366
368
|
# Checks if llms.txt file follows proper format with title, description, and documentation links.
|
|
367
369
|
# Defaults to 'llms.txt' in current directory if no file specified.
|
|
370
|
+
# The config file's docs setting is the docs directory for generate, so it is not used here.
|
|
368
371
|
#
|
|
369
372
|
# @param options [Hash] command options from parse_options
|
|
370
|
-
# @option options [String] :config path to config file
|
|
371
373
|
# @option options [String] :docs path to llms.txt file (defaults to 'llms.txt')
|
|
372
374
|
# @raise [SystemExit] exits with status 1 if file not found or invalid
|
|
373
375
|
def validate(options)
|
|
374
|
-
|
|
375
|
-
config = LlmDocsBuilder::Config.new(options[:config])
|
|
376
|
-
merged_options = config.merge_with_options(options)
|
|
377
|
-
|
|
378
|
-
file_path = merged_options[:docs] || 'llms.txt'
|
|
376
|
+
file_path = options[:docs] || 'llms.txt'
|
|
379
377
|
|
|
380
|
-
unless File.exist?(file_path)
|
|
381
|
-
puts "File not found: #{file_path}"
|
|
382
|
-
exit 1
|
|
383
|
-
end
|
|
378
|
+
exit_with_error("File not found: #{file_path}") unless File.exist?(file_path)
|
|
384
379
|
|
|
385
380
|
content = File.read(file_path)
|
|
386
381
|
valid = LlmDocsBuilder.validate(content)
|
|
@@ -397,6 +392,19 @@ module LlmDocsBuilder
|
|
|
397
392
|
end
|
|
398
393
|
end
|
|
399
394
|
|
|
395
|
+
# Load the config file given with --config, or the auto-discovered one
|
|
396
|
+
#
|
|
397
|
+
# @param config_path [String, nil] path given with --config, or nil to auto-find
|
|
398
|
+
# @return [LlmDocsBuilder::Config] loaded configuration
|
|
399
|
+
# @raise [LlmDocsBuilder::Errors::GenerationError] if the given config file does not exist
|
|
400
|
+
def load_config(config_path)
|
|
401
|
+
if config_path && !File.exist?(config_path)
|
|
402
|
+
raise LlmDocsBuilder::Errors::GenerationError, "Config file not found: #{config_path}"
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
LlmDocsBuilder::Config.new(config_path)
|
|
406
|
+
end
|
|
407
|
+
|
|
400
408
|
# Display version information
|
|
401
409
|
def show_version
|
|
402
410
|
puts "llm-docs-builder version #{LlmDocsBuilder::VERSION}"
|
|
@@ -60,6 +60,8 @@ module LlmDocsBuilder
|
|
|
60
60
|
# @option options [String] :body additional body content
|
|
61
61
|
# @option options [String] :output output file path
|
|
62
62
|
# @option options [Boolean] :convert_urls convert HTML URLs to markdown format
|
|
63
|
+
# @option options [String] :relative_links where `base_url` expansion resolves relative links from:
|
|
64
|
+
# `'base_url'` (default) or `'file'` (the file's directory)
|
|
63
65
|
# @option options [Boolean] :remove_comments remove HTML comments
|
|
64
66
|
# @option options [Boolean] :normalize_whitespace normalize whitespace
|
|
65
67
|
# @option options [Boolean] :remove_badges remove badge images
|
|
@@ -68,7 +70,7 @@ module LlmDocsBuilder
|
|
|
68
70
|
# @option options [String] :suffix suffix for transformed files
|
|
69
71
|
# @option options [Array<String>] :excludes glob patterns for files to exclude
|
|
70
72
|
# @option options [Boolean] :bulk enable bulk transformation mode
|
|
71
|
-
# @option options [Boolean] :include_hidden include hidden files
|
|
73
|
+
# @option options [Boolean] :include_hidden include files in hidden directories (hidden files are always skipped)
|
|
72
74
|
# @option options [Boolean] :remove_code_examples remove code blocks
|
|
73
75
|
# @option options [Boolean] :remove_images remove image syntax
|
|
74
76
|
# @option options [Boolean] :simplify_links simplify link text
|
|
@@ -86,7 +88,8 @@ module LlmDocsBuilder
|
|
|
86
88
|
# @option options [Boolean] :calculate_compression calculate compression ratios
|
|
87
89
|
# @option options [String] :content raw markdown content
|
|
88
90
|
# @option options [String] :source_url source URL for content
|
|
89
|
-
# @return [Hash] merged configuration with CLI overrides applied
|
|
91
|
+
# @return [Hash] merged configuration with CLI overrides applied, marked with
|
|
92
|
+
# `config_merged: true`; the library loads no config file for it unless `:config_file` is given
|
|
90
93
|
def merge_with_options(options)
|
|
91
94
|
# CLI options override config file, config file provides defaults
|
|
92
95
|
{
|
|
@@ -105,6 +108,7 @@ module LlmDocsBuilder
|
|
|
105
108
|
else
|
|
106
109
|
self['convert_urls'] || false
|
|
107
110
|
end,
|
|
111
|
+
relative_links: relative_links_option(options[:relative_links] || self['relative_links']),
|
|
108
112
|
remove_comments: if options.key?(:remove_comments)
|
|
109
113
|
options[:remove_comments]
|
|
110
114
|
elsif !self['remove_comments'].nil?
|
|
@@ -209,6 +213,7 @@ module LlmDocsBuilder
|
|
|
209
213
|
self['calculate_compression'] || false
|
|
210
214
|
end
|
|
211
215
|
}.tap do |merged|
|
|
216
|
+
merged[:config_merged] = true
|
|
212
217
|
merged[:content] = options[:content] if options.key?(:content)
|
|
213
218
|
merged[:source_url] = options[:source_url] if options.key?(:source_url)
|
|
214
219
|
end
|
|
@@ -223,6 +228,22 @@ module LlmDocsBuilder
|
|
|
223
228
|
|
|
224
229
|
private
|
|
225
230
|
|
|
231
|
+
# Accepted values of the relative_links option
|
|
232
|
+
RELATIVE_LINKS = %w[base_url file].freeze
|
|
233
|
+
private_constant :RELATIVE_LINKS
|
|
234
|
+
|
|
235
|
+
# Validate the relative_links option
|
|
236
|
+
#
|
|
237
|
+
# @param value [String, Symbol, nil] configured value
|
|
238
|
+
# @return [String] `'base_url'` or `'file'`
|
|
239
|
+
# @raise [Errors::GenerationError] for any other value
|
|
240
|
+
def relative_links_option(value)
|
|
241
|
+
value = (value || 'base_url').to_s
|
|
242
|
+
return value if RELATIVE_LINKS.include?(value)
|
|
243
|
+
|
|
244
|
+
raise Errors::GenerationError, "Invalid relative_links: #{value} (use base_url or file)"
|
|
245
|
+
end
|
|
246
|
+
|
|
226
247
|
# Find config file in current directory
|
|
227
248
|
#
|
|
228
249
|
# Looks for config files in order of preference:
|
|
@@ -12,6 +12,10 @@ module LlmDocsBuilder
|
|
|
12
12
|
#
|
|
13
13
|
# @api public
|
|
14
14
|
class Generator
|
|
15
|
+
# Maximum length of a file description in llms.txt, in characters
|
|
16
|
+
MAX_DESCRIPTION_LENGTH = 200
|
|
17
|
+
private_constant :MAX_DESCRIPTION_LENGTH
|
|
18
|
+
|
|
15
19
|
# @return [String] path to documentation directory or file
|
|
16
20
|
attr_reader :docs_path
|
|
17
21
|
|
|
@@ -27,6 +31,7 @@ module LlmDocsBuilder
|
|
|
27
31
|
# @option options [String] :description project description (overrides auto-detection)
|
|
28
32
|
# @option options [String] :output output file path for saving
|
|
29
33
|
# @option options [Boolean] :verbose enable verbose output
|
|
34
|
+
# @option options [String] :suffix suffix of bulk-transformed files to skip (default: '.llm')
|
|
30
35
|
def initialize(docs_path, options = {})
|
|
31
36
|
@docs_path = docs_path
|
|
32
37
|
@options = options
|
|
@@ -68,27 +73,11 @@ module LlmDocsBuilder
|
|
|
68
73
|
|
|
69
74
|
# Recursively finds and analyzes markdown files in directory
|
|
70
75
|
#
|
|
71
|
-
# Sorts by priority (README, guides, etc.)
|
|
76
|
+
# Sorts by priority (README, guides, etc.); see MarkdownFinder for the files it skips
|
|
72
77
|
#
|
|
73
78
|
# @return [Array<Hash>] sorted array of analyzed file metadata
|
|
74
79
|
def find_markdown_files_in_directory
|
|
75
|
-
files =
|
|
76
|
-
|
|
77
|
-
Find.find(docs_path) do |path|
|
|
78
|
-
# Skip hidden directories unless explicitly enabled
|
|
79
|
-
# Don't prune the root docs_path itself (even if it's ".")
|
|
80
|
-
if File.directory?(path) && path != docs_path && File.basename(path).start_with?('.') && !options[:include_hidden]
|
|
81
|
-
Find.prune
|
|
82
|
-
next
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
next unless File.file?(path)
|
|
86
|
-
next unless path.match?(/\.md$/i)
|
|
87
|
-
next if File.basename(path).start_with?('.')
|
|
88
|
-
next if should_exclude?(path)
|
|
89
|
-
|
|
90
|
-
files << analyze_file(path)
|
|
91
|
-
end
|
|
80
|
+
files = MarkdownFinder.new(docs_path, options).files.map { |path| analyze_file(path) }
|
|
92
81
|
|
|
93
82
|
files.sort_by { |f| f[:priority] }
|
|
94
83
|
end
|
|
@@ -157,7 +146,8 @@ module LlmDocsBuilder
|
|
|
157
146
|
|
|
158
147
|
# Extracts description from file content
|
|
159
148
|
#
|
|
160
|
-
# Takes first paragraph after title, truncated to
|
|
149
|
+
# Takes first paragraph after title as a single line, truncated to MAX_DESCRIPTION_LENGTH characters.
|
|
150
|
+
# A Markdown link or image that would be cut by the limit is dropped entirely.
|
|
161
151
|
#
|
|
162
152
|
# @param content [String] file content
|
|
163
153
|
# @return [String] extracted description
|
|
@@ -170,12 +160,68 @@ module LlmDocsBuilder
|
|
|
170
160
|
# Get first paragraph
|
|
171
161
|
first_paragraph = description_lines.take_while { |line| !line.strip.empty? }
|
|
172
162
|
|
|
173
|
-
first_paragraph.join(' ').
|
|
163
|
+
description = first_paragraph.join(' ').gsub(/\s+/, ' ').strip
|
|
164
|
+
return description if description.length <= MAX_DESCRIPTION_LENGTH
|
|
165
|
+
|
|
166
|
+
cut_link = link_spans(description).find do |span|
|
|
167
|
+
span.begin < MAX_DESCRIPTION_LENGTH && span.end >= MAX_DESCRIPTION_LENGTH
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
return description.slice(0, MAX_DESCRIPTION_LENGTH) unless cut_link
|
|
171
|
+
|
|
172
|
+
description.slice(0, cut_link.begin).rstrip
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# Finds the character ranges of Markdown links and images in text
|
|
176
|
+
#
|
|
177
|
+
# Brackets and parentheses are matched by nesting depth, so linked images, nested link text, URLs
|
|
178
|
+
# with parentheses and reference links are each one range. An unclosed bracket is not a link.
|
|
179
|
+
#
|
|
180
|
+
# @param text [String] single-line text
|
|
181
|
+
# @return [Array<Range>] inclusive index ranges
|
|
182
|
+
def link_spans(text)
|
|
183
|
+
closing = closing_indexes(text)
|
|
184
|
+
spans = []
|
|
185
|
+
index = 0
|
|
186
|
+
|
|
187
|
+
while (start = text.index('[', index))
|
|
188
|
+
stop = closing[start]
|
|
189
|
+
index = start + 1
|
|
190
|
+
next unless stop
|
|
191
|
+
|
|
192
|
+
stop = closing[stop + 1] while closing[stop + 1]
|
|
193
|
+
start -= 1 if start.positive? && text[start - 1] == '!'
|
|
194
|
+
spans << (start..stop)
|
|
195
|
+
index = stop + 1
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
spans
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
# Maps the index of each opening bracket or parenthesis to the index of the one that closes it
|
|
202
|
+
#
|
|
203
|
+
# @param text [String] text to search
|
|
204
|
+
# @return [Hash{Integer => Integer}] closing index by opening index, unclosed ones are absent
|
|
205
|
+
def closing_indexes(text)
|
|
206
|
+
closing = {}
|
|
207
|
+
open_indexes = { '[' => [], '(' => [] }
|
|
208
|
+
pairs = { ']' => '[', ')' => '(' }
|
|
209
|
+
|
|
210
|
+
text.each_char.with_index do |char, index|
|
|
211
|
+
if open_indexes.key?(char)
|
|
212
|
+
open_indexes[char] << index
|
|
213
|
+
elsif pairs.key?(char) && (opened = open_indexes[pairs[char]].pop)
|
|
214
|
+
closing[opened] = index
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
closing
|
|
174
219
|
end
|
|
175
220
|
|
|
176
221
|
# Assigns priority to file based on filename patterns
|
|
177
222
|
#
|
|
178
|
-
# README gets highest priority, followed by
|
|
223
|
+
# README gets highest priority, followed by getting started docs; other pages are documentation,
|
|
224
|
+
# except tutorials and examples (Examples) and reference and advanced topics (Optional)
|
|
179
225
|
#
|
|
180
226
|
# @param file_path [String] path to file
|
|
181
227
|
# @return [Integer] priority value (1-7, lower is higher priority)
|
|
@@ -186,10 +232,12 @@ module LlmDocsBuilder
|
|
|
186
232
|
return 2 if basename.include?('getting')
|
|
187
233
|
return 3 if basename.include?('guide')
|
|
188
234
|
return 4 if basename.include?('tutorial')
|
|
189
|
-
return
|
|
235
|
+
return 4 if basename.include?('example')
|
|
236
|
+
return 3 if basename.include?('api')
|
|
190
237
|
return 6 if basename.include?('reference')
|
|
238
|
+
return 7 if basename.include?('advanced')
|
|
191
239
|
|
|
192
|
-
|
|
240
|
+
3 # default priority
|
|
193
241
|
end
|
|
194
242
|
|
|
195
243
|
# Applies transformations to content for compression ratio calculation
|
|
@@ -310,13 +358,18 @@ module LlmDocsBuilder
|
|
|
310
358
|
|
|
311
359
|
# Constructs full URL from path using base_url option if provided
|
|
312
360
|
#
|
|
361
|
+
# Each path segment is percent-encoded, so names with spaces, parentheses or
|
|
362
|
+
# non-ASCII characters give a valid Markdown link destination.
|
|
363
|
+
#
|
|
313
364
|
# @param path [String] relative path to file
|
|
314
365
|
# @return [String] full URL or relative path
|
|
315
366
|
def build_url(path)
|
|
367
|
+
encoded_path = path.split('/').map { |segment| ERB::Util.url_encode(segment) }.join('/')
|
|
368
|
+
|
|
316
369
|
if (base_url = options[:base_url])
|
|
317
|
-
File.join(base_url,
|
|
370
|
+
File.join(base_url, encoded_path)
|
|
318
371
|
else
|
|
319
|
-
|
|
372
|
+
encoded_path
|
|
320
373
|
end
|
|
321
374
|
end
|
|
322
375
|
|
|
@@ -335,53 +388,6 @@ module LlmDocsBuilder
|
|
|
335
388
|
end
|
|
336
389
|
end
|
|
337
390
|
|
|
338
|
-
# Tests if file matches any exclusion pattern from options
|
|
339
|
-
#
|
|
340
|
-
# Uses File.fnmatch with pathname and dotmatch flags.
|
|
341
|
-
# Checks against both absolute path and relative path from docs_path.
|
|
342
|
-
#
|
|
343
|
-
# @param file_path [String] path to check
|
|
344
|
-
# @return [Boolean] true if file should be excluded
|
|
345
|
-
def should_exclude?(file_path)
|
|
346
|
-
excludes = Array(options[:excludes])
|
|
347
|
-
return false if excludes.empty?
|
|
348
|
-
|
|
349
|
-
# Get relative path from docs_path for matching
|
|
350
|
-
relative_path = begin
|
|
351
|
-
if File.directory?(docs_path)
|
|
352
|
-
# Convert both to absolute paths first to avoid "different prefix" error
|
|
353
|
-
abs_file = File.expand_path(file_path)
|
|
354
|
-
abs_docs = File.expand_path(docs_path)
|
|
355
|
-
Pathname.new(abs_file).relative_path_from(Pathname.new(abs_docs)).to_s
|
|
356
|
-
else
|
|
357
|
-
File.basename(file_path)
|
|
358
|
-
end
|
|
359
|
-
rescue ArgumentError
|
|
360
|
-
# If paths can't be made relative (different roots), use basename
|
|
361
|
-
File.basename(file_path)
|
|
362
|
-
end
|
|
363
|
-
|
|
364
|
-
excludes.any? do |pattern|
|
|
365
|
-
# Normalize pattern: ensure /** is followed by something
|
|
366
|
-
# fnmatch requires /** to be followed by at least one component
|
|
367
|
-
normalized_pattern = pattern.end_with?('/**') ? "#{pattern}/*" : pattern
|
|
368
|
-
|
|
369
|
-
# Check both absolute and relative paths
|
|
370
|
-
matches = File.fnmatch(normalized_pattern, file_path, File::FNM_PATHNAME | File::FNM_DOTMATCH) ||
|
|
371
|
-
File.fnmatch(normalized_pattern, relative_path, File::FNM_PATHNAME | File::FNM_DOTMATCH)
|
|
372
|
-
|
|
373
|
-
# If pattern starts with **/, also try without it (for root-level matches)
|
|
374
|
-
# Since **/ in fnmatch doesn't match zero directories
|
|
375
|
-
if !matches && normalized_pattern.start_with?('**/')
|
|
376
|
-
pattern_without_prefix = normalized_pattern.sub(%r{^\*\*/}, '')
|
|
377
|
-
matches = File.fnmatch(pattern_without_prefix, file_path, File::FNM_PATHNAME | File::FNM_DOTMATCH) ||
|
|
378
|
-
File.fnmatch(pattern_without_prefix, relative_path, File::FNM_PATHNAME | File::FNM_DOTMATCH)
|
|
379
|
-
end
|
|
380
|
-
|
|
381
|
-
matches
|
|
382
|
-
end
|
|
383
|
-
end
|
|
384
|
-
|
|
385
391
|
# Checks if any transformation options are enabled
|
|
386
392
|
#
|
|
387
393
|
# @return [Boolean] true if any transformation option is enabled
|