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: 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_:\-]+)}/
INDENTED_CODE_LINE =

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

/^ {4,}\S/
VERSION =

Traditional Constant Location

Version::VERSION

Class Method Summary collapse

Class Method Details

.__reset_rake_integrations__Object



114
115
116
117
# File 'lib/yard/fence.rb', line 114

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.



282
283
284
285
286
287
288
# File 'lib/yard/fence.rb', line 282

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/.



254
255
256
257
258
259
260
261
262
# File 'lib/yard/fence.rb', line 254

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.



121
122
123
# File 'lib/yard/fence.rb', line 121

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

.install_rake_tasks!(yard_task_name = :yard) ⇒ Object



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

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



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/yard/fence.rb', line 208

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.



267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/yard/fence.rb', line 267

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.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/yard/fence.rb', line 181

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_in_html_file(html_filepath) ⇒ Object



201
202
203
204
205
206
# File 'lib/yard/fence.rb', line 201

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}}.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/yard/fence.rb', line 137

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
        ln = sanitize_inline_code(line)
        ln = ln.gsub(DOUBLE_BRACE_PLACEHOLDER_REGEX) { |m| fullwidth_braces(m) }
        ln.gsub(SINGLE_BRACE_PLACEHOLDER_REGEX) { |m| fullwidth_braces(m) }
      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
      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.gsub(SINGLE_BRACE_PLACEHOLDER_REGEX) { |m| fullwidth_braces(m) }
    end
  end.join
end

.sanitize_inline_code(line) ⇒ Object

Escape braces inside inline ‘code` spans only.



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

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_text(text) ⇒ Object



174
175
176
177
# File 'lib/yard/fence.rb', line 174

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!‘`).



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/yard/fence.rb', line 230

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