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
|
@@ -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]
|
|
@@ -9,19 +9,40 @@ module LlmDocsBuilder
|
|
|
9
9
|
# @api public
|
|
10
10
|
class LinkTransformer
|
|
11
11
|
include BaseTransformer
|
|
12
|
+
include CodeBlocks
|
|
12
13
|
|
|
13
14
|
# Transform links in content
|
|
14
15
|
#
|
|
15
16
|
# @param content [String] markdown content
|
|
16
17
|
# @param options [Hash] transformation options
|
|
17
18
|
# @option options [String] :base_url base URL for expanding relative links
|
|
19
|
+
# @option options [String] :source_path path of the transformed file relative to the
|
|
20
|
+
# docs root, used to resolve relative links against the file's own directory
|
|
18
21
|
# @option options [Boolean] :convert_urls convert HTML URLs to markdown format
|
|
19
22
|
# @return [String] transformed content
|
|
23
|
+
# @note Code (fenced and indented code blocks, inline code spans) is left unchanged
|
|
20
24
|
def transform(content, options = {})
|
|
21
|
-
|
|
25
|
+
outside_code(content) { |text| transform_outside_code(text, options) }
|
|
26
|
+
end
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
# Transform links in content whose code is already replaced by placeholders
|
|
29
|
+
#
|
|
30
|
+
# @param text [String] content with code replaced by {CodeBlocks} placeholders
|
|
31
|
+
# @param options [Hash] transformation options
|
|
32
|
+
# @option options [String] :base_url base URL for expanding relative links
|
|
33
|
+
# @option options [String] :source_path path of the transformed file relative to the docs root
|
|
34
|
+
# @option options [Boolean] :convert_urls convert HTML URLs to markdown format
|
|
35
|
+
# @option options [Boolean] :simplify_links shorten verbose link text
|
|
36
|
+
# @return [String] transformed content, still with placeholders
|
|
37
|
+
# @api private
|
|
38
|
+
def transform_outside_code(text, options = {})
|
|
39
|
+
result = text
|
|
40
|
+
if options[:base_url]
|
|
41
|
+
result = expand_relative_links(
|
|
42
|
+
result, options[:base_url], options[:source_path], convert_urls: options[:convert_urls]
|
|
43
|
+
)
|
|
44
|
+
end
|
|
45
|
+
result = convert_html_urls(result, convert_definitions: !options[:base_url]) if options[:convert_urls]
|
|
25
46
|
result = simplify_links(result) if options[:simplify_links]
|
|
26
47
|
|
|
27
48
|
result
|
|
@@ -29,47 +50,197 @@ module LlmDocsBuilder
|
|
|
29
50
|
|
|
30
51
|
private
|
|
31
52
|
|
|
32
|
-
#
|
|
33
|
-
|
|
34
|
-
|
|
53
|
+
# A link target with a URI scheme is never a relative path
|
|
54
|
+
URI_SCHEME_PATTERN = /\A[a-z][a-z0-9+.-]*:/i
|
|
55
|
+
private_constant :URI_SCHEME_PATTERN
|
|
56
|
+
|
|
57
|
+
# Contents of a bracket pair in a link text: plain text, images and brackets that do not open a link
|
|
58
|
+
# (links do not nest)
|
|
59
|
+
BRACKET_CONTENT_PATTERN = /(?:[^\[\]]|!\[[^\[\]]*\]|\[[^\[\]]*\](?!\())*/
|
|
60
|
+
|
|
61
|
+
# Part of a link text: brackets in it may be nested up to two levels deep (e.g. a linked image)
|
|
62
|
+
LINK_TEXT_PART_PATTERN = /[^\[\]]|!\[#{BRACKET_CONTENT_PATTERN}\]|\[#{BRACKET_CONTENT_PATTERN}\](?!\()/
|
|
63
|
+
|
|
64
|
+
# Link destination; it never runs into protected code, only its title may contain it
|
|
65
|
+
LINK_DESTINATION_PATTERN = /\(([^)\u0000]+(?:\s+(?:"[^")]*"|'[^')]*'))?)\)/
|
|
66
|
+
|
|
67
|
+
# Inline link
|
|
68
|
+
LINK_PATTERN = /\[((?:#{LINK_TEXT_PART_PATTERN})+)\]#{LINK_DESTINATION_PATTERN}/
|
|
69
|
+
|
|
70
|
+
# Inline link or image inside a link text, where an image may have no alt text (`[](url)`)
|
|
71
|
+
NESTED_LINK_PATTERN = /\[((?:#{LINK_TEXT_PART_PATTERN})*)\]#{LINK_DESTINATION_PATTERN}/
|
|
72
|
+
|
|
73
|
+
# Link reference definition line (`[id]: target "title"`): its label, destination and the rest of the line.
|
|
74
|
+
# Footnote definitions (`[^1]: text`) are not link reference definitions.
|
|
75
|
+
REFERENCE_DEFINITION_PATTERN = /
|
|
76
|
+
^((?:\ {0,3}>\ ?)*\ {0,3}\[(?!\^)(?:[^\[\]\\\n]|\\.)+\]:[ \t]*)
|
|
77
|
+
(<[^<>\n]*>|[^\s<\u0000][^\s\u0000]*)
|
|
78
|
+
((?:[ \t]+(?:"[^"\n]*"|'[^'\n]*'|\([^()\n]*\)))?[ \t\r]*)$
|
|
79
|
+
/x
|
|
80
|
+
|
|
81
|
+
# Consecutive link reference definition lines
|
|
82
|
+
REFERENCE_DEFINITIONS_PATTERN = /#{REFERENCE_DEFINITION_PATTERN}(?:\n#{REFERENCE_DEFINITION_PATTERN})*/
|
|
83
|
+
|
|
84
|
+
# Line that link reference definitions may follow: a blank line, an ATX heading, a thematic break or a code
|
|
85
|
+
# block. A definition cannot interrupt a paragraph.
|
|
86
|
+
REFERENCE_DEFINITIONS_PRECEDING_LINE_PATTERN = /
|
|
87
|
+
(?:\ {0,3}>\ ?)*
|
|
88
|
+
(?:
|
|
89
|
+
[ \t]* |
|
|
90
|
+
\ {0,3}\#{1,6}(?:[ \t][^\n]*)? |
|
|
91
|
+
\ {0,3}(?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,}) |
|
|
92
|
+
#{CODE_BLOCK_PLACEHOLDER_PATTERN}[ \t]*
|
|
93
|
+
)\r?
|
|
94
|
+
/x
|
|
35
95
|
|
|
36
|
-
#
|
|
96
|
+
# Definitions on the first or second line of the content
|
|
97
|
+
LEADING_REFERENCE_DEFINITIONS_PATTERN =
|
|
98
|
+
/\A(?:#{REFERENCE_DEFINITIONS_PRECEDING_LINE_PATTERN}\n)?\K#{REFERENCE_DEFINITIONS_PATTERN}/
|
|
99
|
+
|
|
100
|
+
# Definitions from the third line on, so that they never overlap the leading ones. Kept apart from the leading
|
|
101
|
+
# pattern: an `\A` alternative here would make the regex try every position of the content.
|
|
102
|
+
FOLLOWING_REFERENCE_DEFINITIONS_PATTERN =
|
|
103
|
+
/\n#{REFERENCE_DEFINITIONS_PRECEDING_LINE_PATTERN}\n\K#{REFERENCE_DEFINITIONS_PATTERN}/
|
|
104
|
+
|
|
105
|
+
private_constant :BRACKET_CONTENT_PATTERN, :LINK_TEXT_PART_PATTERN,
|
|
106
|
+
:LINK_DESTINATION_PATTERN, :LINK_PATTERN, :NESTED_LINK_PATTERN, :REFERENCE_DEFINITION_PATTERN,
|
|
107
|
+
:REFERENCE_DEFINITIONS_PATTERN,
|
|
108
|
+
:REFERENCE_DEFINITIONS_PRECEDING_LINE_PATTERN, :LEADING_REFERENCE_DEFINITIONS_PATTERN,
|
|
109
|
+
:FOLLOWING_REFERENCE_DEFINITIONS_PATTERN
|
|
110
|
+
|
|
111
|
+
# Expand relative links and link reference definitions to absolute URLs
|
|
37
112
|
#
|
|
38
113
|
# @param content [String] markdown content
|
|
39
114
|
# @param base_url [String] base URL for expansion
|
|
115
|
+
# @param source_path [String, nil] path of the file relative to the docs root
|
|
116
|
+
# @param convert_urls [Boolean] also convert HTML targets of link reference definitions to markdown, so the
|
|
117
|
+
# definitions are scanned only once
|
|
40
118
|
# @return [String] content with expanded links
|
|
41
|
-
def expand_relative_links(content, base_url)
|
|
42
|
-
content
|
|
43
|
-
|
|
44
|
-
|
|
119
|
+
def expand_relative_links(content, base_url, source_path = nil, convert_urls: false)
|
|
120
|
+
result = expand_links_matching(content, LINK_PATTERN, base_url, source_path)
|
|
121
|
+
|
|
122
|
+
rewrite_reference_definitions(result) do |target|
|
|
123
|
+
expanded = expand_target(target, base_url, source_path)
|
|
124
|
+
convert_urls ? convert_html_target(expanded) : expanded
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Rewrite the destinations of link reference definitions
|
|
129
|
+
#
|
|
130
|
+
# @param content [String] markdown content
|
|
131
|
+
# @yieldparam target [String] destination of a definition
|
|
132
|
+
# @yieldreturn [String] new destination
|
|
133
|
+
# @return [String] content with rewritten definitions
|
|
134
|
+
def rewrite_reference_definitions(content)
|
|
135
|
+
return content unless content.include?(']:')
|
|
45
136
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
"[#{text}](#{expanded_url})"
|
|
137
|
+
rewrite = lambda do |definitions|
|
|
138
|
+
definitions.gsub(REFERENCE_DEFINITION_PATTERN) do
|
|
139
|
+
match = ::Regexp.last_match
|
|
140
|
+
|
|
141
|
+
"#{match[1]}#{yield(match[2])}#{match[3]}"
|
|
52
142
|
end
|
|
53
143
|
end
|
|
144
|
+
|
|
145
|
+
content
|
|
146
|
+
.sub(LEADING_REFERENCE_DEFINITIONS_PATTERN, &rewrite)
|
|
147
|
+
.gsub(FOLLOWING_REFERENCE_DEFINITIONS_PATTERN, &rewrite)
|
|
54
148
|
end
|
|
55
149
|
|
|
56
|
-
#
|
|
150
|
+
# Expand relative links that match a pattern, and the links and images in their link text
|
|
57
151
|
#
|
|
58
|
-
# @param
|
|
59
|
-
# @
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
152
|
+
# @param content [String] markdown content
|
|
153
|
+
# @param pattern [Regexp] pattern of the links to expand
|
|
154
|
+
# @param base_url [String] base URL for expansion
|
|
155
|
+
# @param source_path [String, nil] path of the file relative to the docs root
|
|
156
|
+
# @return [String] content with expanded links
|
|
157
|
+
def expand_links_matching(content, pattern, base_url, source_path)
|
|
158
|
+
content.gsub(pattern) do
|
|
159
|
+
url = ::Regexp.last_match(2)
|
|
160
|
+
text = expand_links_matching(::Regexp.last_match(1), NESTED_LINK_PATTERN, base_url, source_path)
|
|
161
|
+
|
|
162
|
+
"[#{text}](#{expand_target(url, base_url, source_path)})"
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Expand a link target unless it is absolute, protocol-relative or an anchor
|
|
167
|
+
#
|
|
168
|
+
# @param target [String] link target, optionally followed by a link title
|
|
169
|
+
# @param base_url [String] base URL for expansion
|
|
170
|
+
# @param source_path [String, nil] path of the file relative to the docs root
|
|
171
|
+
# @return [String] expanded or unchanged target
|
|
172
|
+
def expand_target(target, base_url, source_path)
|
|
173
|
+
destination = target.delete_prefix('<')
|
|
174
|
+
return target if destination.start_with?('//', '#') || destination.match?(URI_SCHEME_PATTERN)
|
|
175
|
+
|
|
176
|
+
resolve_relative_url(base_url, target, source_path)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
# Resolve a relative link target against the base URL
|
|
180
|
+
#
|
|
181
|
+
# Targets starting with `/` are resolved from the base URL itself, other targets from the source
|
|
182
|
+
# file's directory. `./` and `../` segments are normalized. Titles, queries and anchors are kept.
|
|
183
|
+
# Angle-bracket destinations (`<path with spaces>`) are unwrapped and their spaces percent-encoded.
|
|
184
|
+
# A query-only target (`?tab=2`) resolves to the source file itself, as in RFC 3986, with its
|
|
185
|
+
# name percent-encoded.
|
|
186
|
+
#
|
|
187
|
+
# @param base_url [String] base URL for expansion
|
|
188
|
+
# @param target [String] relative link target, optionally followed by a link title
|
|
189
|
+
# @param source_path [String, nil] path of the source file relative to the docs root
|
|
190
|
+
# @return [String] expanded URL
|
|
191
|
+
def resolve_relative_url(base_url, target, source_path)
|
|
192
|
+
url, title = target.match(/\A(.*?)(\s+(?:"[^"]*"|'[^']*'))?\z/m).captures
|
|
193
|
+
url = url[/\A<(.*)>\z/m, 1]&.gsub(' ', '%20') || url
|
|
194
|
+
path, suffix = url.match(/\A([^?#]*)(.*)\z/m).captures
|
|
195
|
+
query_only = source_path && path.empty? && suffix.start_with?('?')
|
|
196
|
+
path = ERB::Util.url_encode(File.basename(source_path)) if query_only
|
|
197
|
+
source_dir = source_path ? File.dirname(source_path) : '.'
|
|
198
|
+
origin = base_url[%r{\A[^:/?#]+://[^/?#]*}].to_s
|
|
199
|
+
base_path = base_url.delete_prefix(origin)
|
|
200
|
+
dir = path.start_with?('/') ? base_path : File.join(base_path, source_dir)
|
|
201
|
+
|
|
202
|
+
"#{origin}#{normalize_path("#{dir}/#{path}")}#{suffix}#{title}"
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# Normalize `.`, `..` and empty segments of a path
|
|
206
|
+
#
|
|
207
|
+
# @param path [String] path to normalize
|
|
208
|
+
# @return [String] normalized path, with its trailing slash kept
|
|
209
|
+
def normalize_path(path)
|
|
210
|
+
normalized = Pathname.new(path).cleanpath.to_s
|
|
211
|
+
normalized += '/' if path.match?(%r{/\.{0,2}\z}) && !normalized.end_with?('/')
|
|
212
|
+
normalized
|
|
63
213
|
end
|
|
64
214
|
|
|
65
215
|
# Convert HTML URLs to markdown format
|
|
66
216
|
#
|
|
217
|
+
# Converts absolute URLs anywhere in the content and relative link and link reference definition targets,
|
|
218
|
+
# keeping queries and anchors.
|
|
219
|
+
#
|
|
67
220
|
# @param content [String] markdown content
|
|
221
|
+
# @param convert_definitions [Boolean] whether to convert link reference definition targets
|
|
68
222
|
# @return [String] content with converted URLs
|
|
69
|
-
def convert_html_urls(content)
|
|
70
|
-
content.gsub(%r{https?://[^\s
|
|
71
|
-
url.sub(/\.html
|
|
223
|
+
def convert_html_urls(content, convert_definitions: true)
|
|
224
|
+
result = content.gsub(%r{https?://[^\s<>?#]+?\.html?(?=[?#)\s]|$)}) do |url|
|
|
225
|
+
url.sub(/\.html?\z/, '.md')
|
|
72
226
|
end
|
|
227
|
+
|
|
228
|
+
result = result.gsub(/\]\(([^)\s?#]+?)\.html?(?=[?#)\s])/) do |match|
|
|
229
|
+
path = ::Regexp.last_match(1)
|
|
230
|
+
path.match?(URI_SCHEME_PATTERN) ? match : "](#{path}.md"
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
return result unless convert_definitions
|
|
234
|
+
|
|
235
|
+
rewrite_reference_definitions(result) { |target| convert_html_target(target) }
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# Convert a relative HTML link reference definition target to markdown
|
|
239
|
+
#
|
|
240
|
+
# @param target [String] destination of a definition
|
|
241
|
+
# @return [String] converted or unchanged target
|
|
242
|
+
def convert_html_target(target)
|
|
243
|
+
target.match?(URI_SCHEME_PATTERN) ? target : target.sub(/\A([^?#]+?)\.html?(?=[?#]|\z)/, '\1.md')
|
|
73
244
|
end
|
|
74
245
|
|
|
75
246
|
# Simplify verbose link text
|
|
@@ -77,14 +248,14 @@ module LlmDocsBuilder
|
|
|
77
248
|
# @param content [String] markdown content
|
|
78
249
|
# @return [String] content with simplified links
|
|
79
250
|
def simplify_links(content)
|
|
80
|
-
content.gsub(
|
|
251
|
+
content.gsub(LINK_PATTERN) do
|
|
81
252
|
text = ::Regexp.last_match(1)
|
|
82
253
|
url = ::Regexp.last_match(2)
|
|
83
254
|
|
|
84
255
|
simplified_text = text
|
|
85
|
-
.gsub(/^(click here to|see|read more about|check out|visit)\s+(the\s+)
|
|
256
|
+
.gsub(/^(?:(?:click here to|see|read more about|check out|visit)\s+(?:the\s+)?)+/i, '')
|
|
86
257
|
.gsub(/\s+(here|documentation|docs)$/i, '')
|
|
87
|
-
.strip
|
|
258
|
+
.gsub(/\A\s+|\s+\z/, '') # String#strip also removes the NUL of code placeholders
|
|
88
259
|
|
|
89
260
|
simplified_text = text if simplified_text.empty?
|
|
90
261
|
|
data/lib/llm_docs_builder.rb
CHANGED
|
@@ -3,9 +3,11 @@
|
|
|
3
3
|
require 'zeitwerk'
|
|
4
4
|
require 'pathname'
|
|
5
5
|
require 'find'
|
|
6
|
+
require 'strscan'
|
|
6
7
|
|
|
7
8
|
autoload(:Nokogiri, 'nokogiri')
|
|
8
9
|
autoload(:CGI, 'cgi')
|
|
10
|
+
autoload(:ERB, 'erb')
|
|
9
11
|
|
|
10
12
|
loader = Zeitwerk::Loader.for_gem
|
|
11
13
|
loader.inflector.inflect('cli' => 'CLI')
|
|
@@ -55,8 +57,7 @@ module LlmDocsBuilder
|
|
|
55
57
|
docs_path = nil
|
|
56
58
|
end
|
|
57
59
|
|
|
58
|
-
|
|
59
|
-
merged_options = config.merge_with_options(options)
|
|
60
|
+
merged_options = merge_config(options)
|
|
60
61
|
|
|
61
62
|
# Use docs_path param or config file docs setting
|
|
62
63
|
final_docs_path = docs_path || merged_options[:docs]
|
|
@@ -84,8 +85,7 @@ module LlmDocsBuilder
|
|
|
84
85
|
# @example Transform using config file
|
|
85
86
|
# LlmDocsBuilder.transform_markdown('README.md', config_file: 'llm-docs-builder.yml')
|
|
86
87
|
def transform_markdown(file_path, options = {})
|
|
87
|
-
|
|
88
|
-
merged_options = config.merge_with_options(options)
|
|
88
|
+
merged_options = merge_config(options)
|
|
89
89
|
|
|
90
90
|
MarkdownTransformer.new(file_path, merged_options).transform
|
|
91
91
|
end
|
|
@@ -102,6 +102,8 @@ module LlmDocsBuilder
|
|
|
102
102
|
# config)
|
|
103
103
|
# @option options [Array<String>] :excludes glob patterns for files to exclude (overrides
|
|
104
104
|
# config)
|
|
105
|
+
# @option options [Boolean] :include_hidden include files in hidden directories (overrides
|
|
106
|
+
# config)
|
|
105
107
|
# @option options [Boolean] :verbose enable verbose output (overrides config)
|
|
106
108
|
# @return [Array<String>] paths of transformed files
|
|
107
109
|
#
|
|
@@ -115,8 +117,7 @@ module LlmDocsBuilder
|
|
|
115
117
|
# @example Bulk transform using config file
|
|
116
118
|
# LlmDocsBuilder.bulk_transform('./docs', config_file: 'llm-docs-builder.yml')
|
|
117
119
|
def bulk_transform(docs_path, options = {})
|
|
118
|
-
|
|
119
|
-
merged_options = config.merge_with_options(options)
|
|
120
|
+
merged_options = merge_config(options)
|
|
120
121
|
|
|
121
122
|
BulkTransformer.new(docs_path, merged_options).transform_all
|
|
122
123
|
end
|
|
@@ -136,5 +137,20 @@ module LlmDocsBuilder
|
|
|
136
137
|
def validate(content)
|
|
137
138
|
Validator.new(content).valid?
|
|
138
139
|
end
|
|
140
|
+
|
|
141
|
+
private
|
|
142
|
+
|
|
143
|
+
# Merges config file values into options, unless the options are already merged and name no config file
|
|
144
|
+
#
|
|
145
|
+
# @param options [Hash] options, possibly already merged by Config#merge_with_options
|
|
146
|
+
# @option options [String] :config_file path to YAML config file (auto-finds llm-docs-builder.yml)
|
|
147
|
+
# @option options [Boolean] :config_merged set by Config#merge_with_options, skips loading
|
|
148
|
+
# when no :config_file is given
|
|
149
|
+
# @return [Hash] merged options
|
|
150
|
+
def merge_config(options)
|
|
151
|
+
return options if options[:config_merged] && !options.key?(:config_file)
|
|
152
|
+
|
|
153
|
+
Config.new(options[:config_file]).merge_with_options(options)
|
|
154
|
+
end
|
|
139
155
|
end
|
|
140
156
|
end
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
# Example
|
|
2
|
-
# Copy to
|
|
1
|
+
# Example llm-docs-builder.yml configuration file
|
|
2
|
+
# Copy to llm-docs-builder.yml and customize for your project
|
|
3
3
|
|
|
4
4
|
# Path to documentation directory or file
|
|
5
5
|
docs: ./docs
|
|
@@ -11,7 +11,7 @@ base_url: https://myproject.io
|
|
|
11
11
|
title: My Awesome Project
|
|
12
12
|
description: A Ruby library that helps developers build amazing applications
|
|
13
13
|
|
|
14
|
-
# Output file path (optional)
|
|
14
|
+
# Output file path for generate (optional; transform writes a file only with -o)
|
|
15
15
|
output: llms.txt
|
|
16
16
|
|
|
17
17
|
# Transform options (optional)
|
|
@@ -20,7 +20,9 @@ verbose: false # Verbose output
|
|
|
20
20
|
|
|
21
21
|
# Bulk transformation options (optional)
|
|
22
22
|
suffix: .llm # Suffix for transformed files (creates file.llm.md)
|
|
23
|
-
|
|
23
|
+
|
|
24
|
+
# File selection for generate and bulk-transform (optional)
|
|
25
|
+
excludes: # Patterns for files to exclude
|
|
24
26
|
- "**/private/**" # Exclude private directories
|
|
25
27
|
- "**/draft-*.md" # Exclude draft files
|
|
26
|
-
|
|
28
|
+
include_hidden: false # Include files in hidden directories (e.g. .github/); hidden files are always skipped
|