Module: Yard::Fence

Defined in:
lib/yard/fence.rb,
lib/yard/fence/version.rb,
lib/yard/fence/rake_task.rb,
lib/yard/fence/kramdown_gfm_document.rb

Defined Under Namespace

Modules: HtmlHelperPatch, Version Classes: Error, KramdownGfmDocument, RakeTask

Constant Summary collapse

ASCII_BRACES =
"{}"
FULLWIDTH_BRACES =
"{}"
RAKE_INTEGRATIONS =
{}
RAKE_INTEGRATIONS_MUTEX =
Mutex.new
KRAMDOWN_PROVIDER =

IMPORTANT: YARD expects :const to be a String. YARD concatenates with a String prefix (e.g., “::” + const), so a Symbol here will break provider resolution. Some static analyzers suggest making this a Symbol to match types, but that is incorrect for YARD. Do NOT change this to a Symbol.

{lib: :kramdown, const: "KramdownGfmDocument"}
GLOB_PATTERN =
"*.{md,MD,txt,TXT}"
TRIPLE_TICK_FENCE =
/^\s*```/
INLINE_TICK_FENCE =
/`([^`]+)`/
DOUBLE_BRACE_PLACEHOLDER_REGEX =
/{{([^{}]+)}}/
SINGLE_BRACE_PLACEHOLDER_REGEX =
/{([A-Za-z0-9_:-]+)}/
BRACED_TOKEN_REFERENCE_REGEX =
/{([^{}]*(?:\\?\|)[^{}]*)}/
BRACED_HTML_FRAGMENT_REGEX =
/{([^{}]*<\/?[A-Za-z][^{}]*)}/
INDENTED_CODE_LINE =

Lines that are part of a classic indented code block (CommonMark: 4 spaces)

/^ {4,}\S/
HTML_HELPER_TEMPLATE_PATCH =
proc do |options|
  HtmlHelperPatch if options.format == :html
end
VERSION =

Traditional Constant Location

Version::VERSION

Class Method Summary collapse

Class Method Details

.__reset_rake_integrations__Object



116
117
118
119
# File 'lib/yard/fence.rb', line 116

def __reset_rake_integrations__
  RAKE_INTEGRATIONS_MUTEX.synchronize { RAKE_INTEGRATIONS.clear }
  nil
end

.at_load_hookObject

Deprecated.

Use prepare_for_yard instead. This method is kept for backward compatibility but does nothing at load time. Call prepare_for_yard from a rake task.



329
330
331
332
333
334
335
# File 'lib/yard/fence.rb', line 329

def at_load_hook
  # INTENTIONALLY EMPTY
  # Previously this ran at load time, but that caused docs/ to be cleared
  # during unrelated rake tasks like `build` and `release`.
  # All preparation now happens via the yard:fence:prepare rake task.
  nil
end

.clean_docs_directoryObject

Clear the docs output directory to remove stale generated files. Only runs when YARD_FENCE_CLEAN_DOCS=true is set. This ensures that if a markdown file is deleted from the project, its corresponding HTML file won’t remain in docs/.



301
302
303
304
305
306
307
308
309
# File 'lib/yard/fence.rb', line 301

def clean_docs_directory
  return unless ENV.fetch("YARD_FENCE_CLEAN_DOCS", "false").casecmp?("true")

  docs = File.join(Dir.pwd, "docs")
  return unless Dir.exist?(docs)

  FileUtils.rm_rf(docs)
  puts "[yard/fence] Cleared docs/ directory (YARD_FENCE_CLEAN_DOCS=true)"
end

.fullwidth_braces(str) ⇒ Object

Replace ASCII { } with Unicode fullwidth { }. Visual effect in browsers is the same, but YARD’s link matcher won’t recognize them.



123
124
125
# File 'lib/yard/fence.rb', line 123

def fullwidth_braces(str)
  str.tr(ASCII_BRACES, FULLWIDTH_BRACES)
end

.install_html_helper_patch!Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/yard/fence.rb', line 271

def install_html_helper_patch!
  begin
    require "yard" unless defined?(::YARD::Templates::Template)
    require "yard/templates/helpers/html_helper" unless defined?(::YARD::Templates::Helpers::HtmlHelper)
  rescue LoadError, NameError => e
    warn("Yard::Fence.install_html_helper_patch!: failed to load YARD HTML helper: #{e.class}: #{e.message}")
    return false
  end

  extra_includes = ::YARD::Templates::Template.extra_includes
  extra_includes << HTML_HELPER_TEMPLATE_PATCH unless extra_includes.include?(HTML_HELPER_TEMPLATE_PATCH)

  helper = ::YARD::Templates::Helpers::HtmlHelper
  return true if helper.method_defined?(:yard_fence_unprotected_resolve_links)

  helper.module_eval do
    alias_method(:yard_fence_unprotected_resolve_links, :resolve_links)

    def resolve_links(text)
      protected_text = ::Yard::Fence.sanitize_prose_braces(text.to_s)
      ::Yard::Fence.restore_ascii_braces(yard_fence_unprotected_resolve_links(protected_text))
    end
  end
  true
end

.install_rake_tasks!(yard_task_name = :yard) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/yard/fence.rb', line 90

def install_rake_tasks!(yard_task_name = :yard)
  return false unless defined?(::Rake::Task)

  require_relative "fence/rake_task"

  ::Yard::Fence::RakeTask.new unless ::Rake::Task.task_defined?("yard:fence:prepare")
  return true unless ::Rake::Task.task_defined?(yard_task_name)

  yard_task = ::Rake::Task[yard_task_name]
  prereqs = yard_task.prerequisites.map(&:to_s)
  yard_task.enhance(["yard:fence:prepare"]) unless prereqs.include?("yard:fence:prepare")

  RAKE_INTEGRATIONS_MUTEX.synchronize do
    key = yard_task_name.to_s
    unless RAKE_INTEGRATIONS[key]
      yard_task.enhance { ::Yard::Fence.postprocess_html_docs }
      RAKE_INTEGRATIONS[key] = true
    end
  end

  true
rescue LoadError => e
  warn("Yard::Fence.install_rake_tasks!: failed to load rake integration: #{e.class}: #{e.message}")
  false
end

.postprocess_html_docsObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/yard/fence.rb', line 218

def postprocess_html_docs
  if ENV.fetch("YARD_FENCE_DISABLE", "false").casecmp?("true")
    # :nocov:
    warn("[yard/fence] postprocess_html_docs disabled via YARD_FENCE_DISABLE")
    # :nocov:
  else
    docs = File.join(Dir.pwd, "docs")
    return unless Dir.exist?(docs)
    Dir.glob(File.join(docs, "**", "*.html")).each do |html|
      restore_ascii_braces_in_html_file(html)
    end
  end
rescue => e
  warn("Yard::Fence.postprocess_html_docs failed: #{e.class}: #{e.message}")
end

.prepare_for_yardObject

Prepare for YARD documentation generation. This method should be called from a rake task BEFORE yard runs, not at load time. It cleans the docs directory (if YARD_FENCE_CLEAN_DOCS=true) and prepares tmp files.



314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/yard/fence.rb', line 314

def prepare_for_yard
  if ENV.fetch("YARD_FENCE_DISABLE", "false").casecmp?("true")
    # :nocov:
    warn("[yard/fence] prepare_for_yard disabled via YARD_FENCE_DISABLE")
    # :nocov:
  else
    Yard::Fence.clean_docs_directory
    Yard::Fence.prepare_tmp_files
  end
rescue => e
  warn("Yard::Fence: failed to prepare for YARD: #{e.class}: #{e.message}")
end

.prepare_tmp_filesObject

Copy top-level .md/.txt into tmp/yard-fence/ with the above sanitization applied. Clears the staging directory first to prevent stale files from persisting.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/yard/fence.rb', line 187

def prepare_tmp_files
  root = Dir.pwd
  outdir = File.join(root, "tmp", "yard-fence")

  # Clear existing staging directory to prevent stale files from persisting.
  # This ensures that if a markdown file is deleted from the project root,
  # it won't remain in tmp/yard-fence/ and get included in documentation.
  FileUtils.rm_rf(outdir)
  FileUtils.mkdir_p(outdir)

  candidates = Dir.glob(File.join(root, GLOB_PATTERN))
  candidates.each do |src|
    next unless File.file?(src)
    content = File.read(src)
    sanitized = sanitize_text(content)
    dst = File.join(outdir, File.basename(src))
    File.write(dst, sanitized)
  end
end

.restore_ascii_braces(text) ⇒ Object



214
215
216
# File 'lib/yard/fence.rb', line 214

def restore_ascii_braces(text)
  text.tr(FULLWIDTH_BRACES, ASCII_BRACES)
end

.restore_ascii_braces_in_html_file(html_filepath) ⇒ Object



207
208
209
210
211
212
# File 'lib/yard/fence.rb', line 207

def restore_ascii_braces_in_html_file(html_filepath)
  return unless File.file?(html_filepath)
  content = File.read(html_filepath)
  restored = content.tr(FULLWIDTH_BRACES, ASCII_BRACES)
  File.write(html_filepath, restored)
end

.sanitize_fenced_blocks(text) ⇒ Object

Walk the text, toggling a simple in_fence state on “‘ lines. While inside a fence, convert braces to fullwidth; outside, also sanitize inline code and disarm simple prose placeholders like {issuer} or {{something}}.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/yard/fence.rb', line 149

def sanitize_fenced_blocks(text)
  in_fence = false
  in_indented_block = false

  text.each_line.map do |line|
    if line.match?(TRIPLE_TICK_FENCE)
      # Toggle fenced block state; leaving indented block if switching into explicit fence
      in_fence = !in_fence
      in_indented_block = false if in_fence
      line
    elsif in_fence
      fullwidth_braces(line)
    elsif in_indented_block
      # Continue indented block until a blank line or non-indented line breaks it
      if line.strip.empty? || !line.match?(INDENTED_CODE_LINE)
        in_indented_block = false
        # Process this line as normal prose outside block
        sanitize_prose_braces(line)
      else
        fullwidth_braces(line)
      end
    elsif line.match?(INDENTED_CODE_LINE)
      # Enter indented code block on first qualifying line
      in_indented_block = true
      fullwidth_braces(line)
    else
      sanitize_prose_braces(line)
    end
  end.join
end

.sanitize_inline_code(line) ⇒ Object

Escape braces inside inline ‘code` spans only.



128
129
130
131
132
133
134
# File 'lib/yard/fence.rb', line 128

def sanitize_inline_code(line)
  # Use Regexp.last_match to safely access capture; to_s guards against nil
  line.gsub(INLINE_TICK_FENCE) do |_|
    inner = Regexp.last_match(1).to_s
    "`#{fullwidth_braces(inner)}`"
  end
end

.sanitize_prose_braces(line) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/yard/fence.rb', line 136

def sanitize_prose_braces(line)
  ln = sanitize_inline_code(line)
  # IMPORTANT: handle double-brace placeholders first so we don't partially
  # convert the inner {TOKEN} and leave outer ASCII braces from `{{TOKEN}}`.
  ln = ln.gsub(DOUBLE_BRACE_PLACEHOLDER_REGEX) { |m| fullwidth_braces(m) }
  ln = ln.gsub(BRACED_TOKEN_REFERENCE_REGEX) { |m| fullwidth_braces(m) }
  ln = ln.gsub(BRACED_HTML_FRAGMENT_REGEX) { |m| fullwidth_braces(m) }
  ln.gsub(SINGLE_BRACE_PLACEHOLDER_REGEX) { |m| fullwidth_braces(m) }
end

.sanitize_text(text) ⇒ Object



180
181
182
183
# File 'lib/yard/fence.rb', line 180

def sanitize_text(text)
  return text unless text.is_a?(String)
  sanitize_fenced_blocks(text)
end

.use_kramdown_gfm!Object

Register Kramdown GFM as the highest priority Markdown provider in YARD. Returns true if registration succeeded, false otherwise.

This method is intentionally not executed at file load to avoid circular require warnings when YARD is in the middle of loading itself. Call this from a file loaded via .yardopts (e.g. ‘-e ’require “yard/fence/kramdown_gfm”; Yard::Fence.use_kramdown_gfm!‘`).



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/yard/fence.rb', line 240

def use_kramdown_gfm!
  # :nocov:
  # Not covering, because kramdown support is tested, so this rescue is not hit in test runs.
  unless defined?(Yard::Fence::KramdownGfmDocument)
    raise Error, "Yard::Fence: Kramdown GFM provider not loaded. Add kramdown and kramdown-parser-gfm to your Gemfile."
  end
  # :nocov:
  providers = ::YARD::Templates::Helpers::MarkupHelper::MARKUP_PROVIDERS[:markdown]
  # NOTE: Intentionally using String for :const in KRAMDOWN_PROVIDER.
  # YARD performs string concatenation with this value (e.g., "::" + const).
  # Changing it to a Symbol to satisfy static type hints breaks runtime behavior.
  # Suppress type fuzz complaints: expected Hash{Symbol->Symbol}, actual includes String by design.
  providers.unshift(KRAMDOWN_PROVIDER)
  providers.uniq! { |p| [p[:lib].to_s, p[:const].to_s] }
  true
rescue NameError => e
  warn("Yard::Fence.use_kramdown_gfm!: failed to load YARD helper: #{e.class}: #{e.message}")
  false
end