Module: WikiPromoter

Defined in:
lib/wiki_promoter.rb,
lib/wiki_promoter/version.rb,
lib/wiki_promoter/migrator.rb,
lib/wiki_promoter/publisher.rb

Defined Under Namespace

Classes: Error, Migrator, Publisher, TreeNode

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

Percent-encode a wiki page name for use as a markdown link target. GitHub wiki renders [text](page name) as plain text when the URL contains unencoded reserved characters: '#' starts a fragment, '%' creates invalid escape sequences, '(' ')' break the markdown link syntax. Encode '%' first to avoid double-encoding.



15
16
17
18
19
20
21
22
# File 'lib/wiki_promoter.rb', line 15

def self.encode_wiki_link_target(wiki_name)
  wiki_name
    .gsub("%", "%25")
    .gsub(" ", "%20")
    .gsub("#", "%23")
    .gsub("(", "%28")
    .gsub(")", "%29")
end

.repoint_references(content, docs_path:, entry_url:, repository: nil, page_urls: {}, github_host: "https://github.com") ⇒ Object

Targeted literal find-and-replace for repointing references to a just-removed docs tree (bare relative path or a full GitHub blob permalink into it) at the new wiki pages. String substitution only, not free-form prose rewriting -- keeps this operation's side effects predictable.

page_urls maps a specific source file path (e.g. "docs/77-slug/research/results.md") to its own flattened wiki URL. Those deep links are repointed at their specific sub-page (longest path first, so a nested file wins over its parent directory) before the whole docs_path tree falls back to entry_url. When page_urls is empty the behavior is identical to a single docs_path -> entry_url substitution.



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/wiki_promoter/migrator.rb', line 296

def self.repoint_references(content, docs_path:, entry_url:, repository: nil, page_urls: {}, github_host: "https://github.com")
  substitutions = page_urls
    .sort_by { |path, _| -path.length }
    .push([docs_path, entry_url])

  substitutions.reduce(content) do |text, (path, url)|
    pattern = if repository
      %r{
        (?:#{Regexp.escape(github_host)}/#{Regexp.escape(repository)}/blob/[^)\s"'\]]+/)?
        #{Regexp.escape(path)}
        [^)\s"'\]]*
      }x
    else
      # If no repository specified, only match bare relative paths
      %r{#{Regexp.escape(path)}[^)\s"'\]]*}
    end
    text.gsub(pattern, url)
  end
end