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.
@@ -9,6 +9,7 @@ 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
  #
@@ -21,17 +22,30 @@ module LlmDocsBuilder
21
22
  # @return [String] transformed content
22
23
  # @note Code (fenced and indented code blocks, inline code spans) is left unchanged
23
24
  def transform(content, options = {})
24
- outside_code(content) do |result|
25
- if options[:base_url]
26
- result = expand_relative_links(
27
- result, options[:base_url], options[:source_path], convert_urls: options[:convert_urls]
28
- )
29
- end
30
- result = convert_html_urls(result, convert_definitions: !options[:base_url]) if options[:convert_urls]
31
- result = simplify_links(result) if options[:simplify_links]
25
+ outside_code(content) { |text| transform_outside_code(text, options) }
26
+ end
32
27
 
33
- result
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
+ )
34
44
  end
45
+ result = convert_html_urls(result, convert_definitions: !options[:base_url]) if options[:convert_urls]
46
+ result = simplify_links(result) if options[:simplify_links]
47
+
48
+ result
35
49
  end
36
50
 
37
51
  private
@@ -40,21 +54,6 @@ module LlmDocsBuilder
40
54
  URI_SCHEME_PATTERN = /\A[a-z][a-z0-9+.-]*:/i
41
55
  private_constant :URI_SCHEME_PATTERN
42
56
 
43
- # Placeholder that stands in for a protected code fragment; a code block's ends with `b`
44
- CODE_PLACEHOLDER = /\u0000(\d+)b?\u0000/
45
-
46
- # Inline code span: a backtick run closed by a run of the same length, within one paragraph
47
- INLINE_CODE = /(?<![`\\])(`+)(?!`)(?:(?!\n[ \t]*\n)[^\u0000])+?(?<!`)\1(?!`)/m
48
-
49
- # Opening line of a fenced code block
50
- FENCE_OPENING = /\A[ \t]*(`{3,}|~{3,})/
51
-
52
- # Blockquote markers at the start of a line
53
- BLOCKQUOTE_PREFIX = /\A(?: {0,3}> ?)*/
54
-
55
- # Line that starts a list item
56
- LIST_ITEM = /\A {0,3}(?:[-+*]|\d{1,9}[.)])(?:[ \t]|\s*\z)/
57
-
58
57
  # Contents of a bracket pair in a link text: plain text, images and brackets that do not open a link
59
58
  # (links do not nest)
60
59
  BRACKET_CONTENT_PATTERN = /(?:[^\[\]]|!\[[^\[\]]*\]|\[[^\[\]]*\](?!\())*/
@@ -90,7 +89,7 @@ module LlmDocsBuilder
90
89
  [ \t]* |
91
90
  \ {0,3}\#{1,6}(?:[ \t][^\n]*)? |
92
91
  \ {0,3}(?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,}) |
93
- \u0000\d+b\u0000[ \t]*
92
+ #{CODE_BLOCK_PLACEHOLDER_PATTERN}[ \t]*
94
93
  )\r?
95
94
  /x
96
95
 
@@ -103,128 +102,12 @@ module LlmDocsBuilder
103
102
  FOLLOWING_REFERENCE_DEFINITIONS_PATTERN =
104
103
  /\n#{REFERENCE_DEFINITIONS_PRECEDING_LINE_PATTERN}\n\K#{REFERENCE_DEFINITIONS_PATTERN}/
105
104
 
106
- private_constant :CODE_PLACEHOLDER, :INLINE_CODE, :FENCE_OPENING, :BLOCKQUOTE_PREFIX, :LIST_ITEM,
107
- :BRACKET_CONTENT_PATTERN, :LINK_TEXT_PART_PATTERN, :LINK_DESTINATION_PATTERN, :LINK_PATTERN,
108
- :NESTED_LINK_PATTERN, :REFERENCE_DEFINITION_PATTERN, :REFERENCE_DEFINITIONS_PATTERN,
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,
109
108
  :REFERENCE_DEFINITIONS_PRECEDING_LINE_PATTERN, :LEADING_REFERENCE_DEFINITIONS_PATTERN,
110
109
  :FOLLOWING_REFERENCE_DEFINITIONS_PATTERN
111
110
 
112
- # Run a transformation on content with code replaced by placeholders, then restore the code
113
- #
114
- # @param content [String] markdown content
115
- # @yieldparam text [String] content with code fragments replaced by placeholders
116
- # @yieldreturn [String] transformed content
117
- # @return [String] transformed content with the original code restored
118
- def outside_code(content)
119
- codes = []
120
- protect = lambda do |code, block: false|
121
- codes << code
122
- "\u0000#{codes.size - 1}#{'b' if block}\u0000"
123
- end
124
-
125
- text = protect_code_blocks(content, protect)
126
- text = text.gsub(INLINE_CODE) { protect.call(::Regexp.last_match(0)) }
127
-
128
- yield(text).gsub(CODE_PLACEHOLDER) { codes[::Regexp.last_match(1).to_i] }
129
- end
130
-
131
- # Replace fenced and indented code blocks with placeholders
132
- #
133
- # Indented lines count as code only after a blank line and outside lists, where they would otherwise be
134
- # paragraph or list item continuations. Code inside blockquotes is detected after the `>` markers.
135
- #
136
- # @param content [String] markdown content
137
- # @param protect [Proc] stores a code fragment and returns its placeholder
138
- # @return [String] content with code blocks replaced
139
- def protect_code_blocks(content, protect)
140
- out = +''
141
- code = +''
142
- fence = nil
143
- fence_depth = 0
144
- depth = 0
145
- in_list = false
146
- prev_blank = true
147
-
148
- content.each_line do |line|
149
- quote = line[BLOCKQUOTE_PREFIX]
150
- body = line.delete_prefix(quote)
151
- prev_blank ||= quote.count('>') > depth
152
- depth = quote.count('>')
153
- fence = nil if depth < fence_depth
154
-
155
- if fence
156
- code << line
157
- closing = line.sub(/\A(?: {0,3}> ?){#{fence_depth}}/, '')
158
- fence = nil if closing.match?(/\A[ \t]*#{Regexp.escape(fence)}#{Regexp.escape(fence[0])}*\s*\z/)
159
- prev_blank = true
160
- next
161
- end
162
-
163
- if !in_list && (prev_blank || !code.empty?) && indented_code?(body)
164
- code << line
165
- next
166
- end
167
-
168
- if (fence = fence_opening(body))
169
- fence_depth = depth
170
- code << line
171
- next
172
- end
173
-
174
- out << protect_lines(code, protect) << line
175
- code = +''
176
- in_list = in_list_after?(body, in_list, prev_blank)
177
- prev_blank = body.strip.empty?
178
- end
179
-
180
- out << protect_lines(code, protect)
181
- end
182
-
183
- # Fence of a line that opens a fenced code block
184
- #
185
- # @param line [String] markdown line
186
- # @return [String, nil] the opening fence, or nil when the line does not open a block
187
- def fence_opening(line)
188
- opening = line.match(FENCE_OPENING)
189
- return unless opening
190
- return if opening[1].start_with?('`') && opening.post_match.include?('`')
191
-
192
- opening[1]
193
- end
194
-
195
- # Check whether a line is indented enough to be code
196
- #
197
- # @param line [String] markdown line
198
- # @return [Boolean] true for a non-blank line indented by four spaces or a tab
199
- def indented_code?(line)
200
- line.match?(/\A(?: {4}|\t)\s*\S/)
201
- end
202
-
203
- # Check whether content after a line is still inside a list
204
- #
205
- # @param line [String] markdown line
206
- # @param in_list [Boolean] whether the previous line was inside a list
207
- # @param prev_blank [Boolean] whether the previous line was blank
208
- # @return [Boolean] true when the list continues after this line
209
- def in_list_after?(line, in_list, prev_blank)
210
- return true if line.match?(LIST_ITEM)
211
- return in_list if line.strip.empty? || !prev_blank || line.match?(/\A[ \t]/)
212
-
213
- false
214
- end
215
-
216
- # Replace collected code lines with a placeholder, keeping the final line break
217
- #
218
- # @param code [String] collected code lines
219
- # @param protect [Proc] stores a code fragment and returns its placeholder
220
- # @return [String] placeholder, or an empty string when there is no code
221
- def protect_lines(code, protect)
222
- return '' if code.empty?
223
-
224
- body = code.chomp
225
- "#{protect.call(body, block: true)}#{code.delete_prefix(body)}"
226
- end
227
-
228
111
  # Expand relative links and link reference definitions to absolute URLs
229
112
  #
230
113
  # @param content [String] markdown content
@@ -2,5 +2,5 @@
2
2
 
3
3
  module LlmDocsBuilder
4
4
  # Current version of the LlmDocsBuilder gem
5
- VERSION = '1.1.0'
5
+ VERSION = '1.1.1'
6
6
  end
@@ -3,6 +3,7 @@
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')
@@ -56,8 +57,7 @@ module LlmDocsBuilder
56
57
  docs_path = nil
57
58
  end
58
59
 
59
- config = Config.new(options[:config_file])
60
- merged_options = config.merge_with_options(options)
60
+ merged_options = merge_config(options)
61
61
 
62
62
  # Use docs_path param or config file docs setting
63
63
  final_docs_path = docs_path || merged_options[:docs]
@@ -85,8 +85,7 @@ module LlmDocsBuilder
85
85
  # @example Transform using config file
86
86
  # LlmDocsBuilder.transform_markdown('README.md', config_file: 'llm-docs-builder.yml')
87
87
  def transform_markdown(file_path, options = {})
88
- config = Config.new(options[:config_file])
89
- merged_options = config.merge_with_options(options)
88
+ merged_options = merge_config(options)
90
89
 
91
90
  MarkdownTransformer.new(file_path, merged_options).transform
92
91
  end
@@ -103,6 +102,8 @@ module LlmDocsBuilder
103
102
  # config)
104
103
  # @option options [Array<String>] :excludes glob patterns for files to exclude (overrides
105
104
  # config)
105
+ # @option options [Boolean] :include_hidden include files in hidden directories (overrides
106
+ # config)
106
107
  # @option options [Boolean] :verbose enable verbose output (overrides config)
107
108
  # @return [Array<String>] paths of transformed files
108
109
  #
@@ -116,8 +117,7 @@ module LlmDocsBuilder
116
117
  # @example Bulk transform using config file
117
118
  # LlmDocsBuilder.bulk_transform('./docs', config_file: 'llm-docs-builder.yml')
118
119
  def bulk_transform(docs_path, options = {})
119
- config = Config.new(options[:config_file])
120
- merged_options = config.merge_with_options(options)
120
+ merged_options = merge_config(options)
121
121
 
122
122
  BulkTransformer.new(docs_path, merged_options).transform_all
123
123
  end
@@ -137,5 +137,20 @@ module LlmDocsBuilder
137
137
  def validate(content)
138
138
  Validator.new(content).valid?
139
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
140
155
  end
141
156
  end
@@ -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
- excludes: # Patterns for files to exclude from bulk transformation
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
- - "**/.*" # Exclude hidden files
28
+ include_hidden: false # Include files in hidden directories (e.g. .github/); hidden files are always skipped
data/package-lock.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "name": "llm-docs-builder",
9
9
  "version": "1.0.0",
10
10
  "devDependencies": {
11
- "lostconf": "0.4.0"
11
+ "lostconf": "0.5.0"
12
12
  }
13
13
  },
14
14
  "node_modules/@nodelib/fs.scandir": {
@@ -62,29 +62,6 @@
62
62
  "node": ">=8"
63
63
  }
64
64
  },
65
- "node_modules/chalk": {
66
- "version": "5.6.2",
67
- "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz",
68
- "integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==",
69
- "dev": true,
70
- "license": "MIT",
71
- "engines": {
72
- "node": "^12.17.0 || ^14.13 || >=16.0.0"
73
- },
74
- "funding": {
75
- "url": "https://github.com/chalk/chalk?sponsor=1"
76
- }
77
- },
78
- "node_modules/commander": {
79
- "version": "12.1.0",
80
- "resolved": "https://registry.npmjs.org/commander/-/commander-12.1.0.tgz",
81
- "integrity": "sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==",
82
- "dev": true,
83
- "license": "MIT",
84
- "engines": {
85
- "node": ">=18"
86
- }
87
- },
88
65
  "node_modules/fast-glob": {
89
66
  "version": "3.3.3",
90
67
  "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.3.3.tgz",
@@ -172,16 +149,14 @@
172
149
  }
173
150
  },
174
151
  "node_modules/lostconf": {
175
- "version": "0.4.0",
176
- "resolved": "https://registry.npmjs.org/lostconf/-/lostconf-0.4.0.tgz",
177
- "integrity": "sha512-VNbUnirRU7uESqMHslIRHTcuyx/rr4OZK+L7EQXtYUe5PorBgqBYvPu+6xOr0CoUy4n34NNUKO6BBH6TgwwGTA==",
152
+ "version": "0.5.0",
153
+ "resolved": "https://registry.npmjs.org/lostconf/-/lostconf-0.5.0.tgz",
154
+ "integrity": "sha512-zl6i9RUkaLkGWOJoIHQE387fjXGBONUe8tUznAMCNw2oCj763hUwDVtIWIX2YH7BETxGRedFbGm2+aXqHNinpQ==",
178
155
  "dev": true,
179
156
  "license": "MIT",
180
157
  "dependencies": {
181
- "chalk": "^5.3.0",
182
- "commander": "^12.1.0",
183
158
  "fast-glob": "^3.3.2",
184
- "micromatch": "^4.0.8",
159
+ "picomatch": "^2.3.1",
185
160
  "smol-toml": "^1.3.0",
186
161
  "yaml": "^2.5.0"
187
162
  },
@@ -189,7 +164,7 @@
189
164
  "lostconf": "dist/cli.js"
190
165
  },
191
166
  "engines": {
192
- "node": ">=18.0.0"
167
+ "node": ">=18.3.0"
193
168
  }
194
169
  },
195
170
  "node_modules/merge2": {
@@ -286,9 +261,9 @@
286
261
  }
287
262
  },
288
263
  "node_modules/smol-toml": {
289
- "version": "1.9.0",
290
- "resolved": "https://registry.npmjs.org/smol-toml/-/smol-toml-1.9.0.tgz",
291
- "integrity": "sha512-hpd+HLON7HdZXqYchMM/+LaTTbdK0AU3NngIJ4KVyWbY9bfQqdL9cD+4yf6dUoU2Ap4VsU0JkQi6FxAI1B2mXQ==",
264
+ "version": "1.9.1",
265
+ "resolved": "https://registry.npmjs.org/smol-toml/-/smol-toml-1.9.1.tgz",
266
+ "integrity": "sha512-j7WyPdf0CJoutMzMoPLF/bVXHtKZIT9Rb4eGeEbWw8X2t5oirjPgpL+OpaENZrILYaXqYwnVH+qgUwVbl+pUJQ==",
292
267
  "dev": true,
293
268
  "license": "BSD-3-Clause",
294
269
  "engines": {
data/package.json CHANGED
@@ -4,6 +4,6 @@
4
4
  "description": "CI dependencies for llm-docs-builder",
5
5
  "private": true,
6
6
  "devDependencies": {
7
- "lostconf": "0.4.0"
7
+ "lostconf": "0.5.0"
8
8
  }
9
9
  }
data/renovate.json CHANGED
@@ -53,5 +53,11 @@
53
53
  "schedule": [
54
54
  "before 4am on the first day of the month"
55
55
  ]
56
+ },
57
+ "osvVulnerabilityAlerts": true,
58
+ "vulnerabilityAlerts": {
59
+ "labels": [
60
+ "security"
61
+ ]
56
62
  }
57
63
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: llm-docs-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maciej Mensfeld
@@ -123,9 +123,11 @@ extensions: []
123
123
  extra_rdoc_files: []
124
124
  files:
125
125
  - ".dockerignore"
126
+ - ".github/CODEOWNERS"
126
127
  - ".github/workflows/ci.yml"
127
128
  - ".github/workflows/docker.yml"
128
129
  - ".github/workflows/push.yml"
130
+ - ".github/workflows/verify-action-pins.yml"
129
131
  - ".gitignore"
130
132
  - ".rspec"
131
133
  - ".rubocop.yml"
@@ -154,12 +156,14 @@ files:
154
156
  - lib/llm_docs_builder/html_to_markdown/figure_code_block_renderer.rb
155
157
  - lib/llm_docs_builder/html_to_markdown/table_markup_renderer.rb
156
158
  - lib/llm_docs_builder/html_to_markdown_converter.rb
159
+ - lib/llm_docs_builder/markdown_finder.rb
157
160
  - lib/llm_docs_builder/markdown_transformer.rb
158
161
  - lib/llm_docs_builder/output_formatter.rb
159
162
  - lib/llm_docs_builder/parser.rb
160
163
  - lib/llm_docs_builder/text_compressor.rb
161
164
  - lib/llm_docs_builder/token_estimator.rb
162
165
  - lib/llm_docs_builder/transformers/base_transformer.rb
166
+ - lib/llm_docs_builder/transformers/code_blocks.rb
163
167
  - lib/llm_docs_builder/transformers/content_cleanup_transformer.rb
164
168
  - lib/llm_docs_builder/transformers/enhancement_transformer.rb
165
169
  - lib/llm_docs_builder/transformers/heading_transformer.rb