Module: Jekyll::L10n::UrlPathBuilder

Defined in:
lib/jekyll-l10n/utils/url_path_builder.rb

Overview

Builds file paths from URLs and vice versa.

UrlPathBuilder provides conversions between Jekyll URLs and file system paths, handling root paths specially and normalizing separators. It supports both regular URLs and PO page paths used for translation files.

Key responsibilities:

  • Normalize URLs (strip leading/trailing slashes)

  • Convert URLs to file paths

  • Convert URLs to PO page paths

  • Calculate relative paths from absolute paths

Examples:

file_path = UrlPathBuilder.url_to_file_path('/docs/page.html')
# Returns 'docs/page.html/index.html'

Class Method Summary collapse

Class Method Details

.normalize_url(url) ⇒ String

Normalize a URL by removing leading and trailing slashes.

Parameters:

  • url (String)

    URL to normalize

Returns:

  • (String)

    Normalized URL



27
28
29
# File 'lib/jekyll-l10n/utils/url_path_builder.rb', line 27

def normalize_url(url)
  url.sub(%r{^/}, '').sub(%r{/$}, '')
end

.relative_path(file_path, dest) ⇒ String

Calculate relative path from destination directory.

Parameters:

  • file_path (String)

    Absolute file path

  • dest (String)

    Destination directory to remove from path

Returns:

  • (String)

    Relative path



69
70
71
# File 'lib/jekyll-l10n/utils/url_path_builder.rb', line 69

def relative_path(file_path, dest)
  file_path.sub(dest, '').sub(%r{^/}, '')
end

.url_to_file_path(url) ⇒ String

Convert a URL to a file system path.

Converts Jekyll URL to the path it would be written to on disk. For URLs with file extensions (assets), preserves the extension. For directory-style URLs, uses index.html.

Parameters:

  • url (String)

    Jekyll URL (e.g., ‘/docs/page.html’ or ‘/assets/style.css’)

Returns:

  • (String)

    File path (e.g., ‘docs/page.html/index.html’ or ‘assets/style.css’)



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jekyll-l10n/utils/url_path_builder.rb', line 39

def url_to_file_path(url)
  normalized = normalize_url(url)

  # Preserve file extensions for assets (css, js, etc.)
  # Only add /index.html for directory-style URLs
  if /\.[a-zA-Z0-9]+$/.match?(normalized)
    normalized
  else
    "#{normalized}/index.html"
  end
end

.url_to_po_page_path(url) ⇒ String

Convert a URL to a PO page path for translation files.

Handles root path specially (converts to ‘index’). Used when saving page-specific translation files.

Parameters:

  • url (String)

    Jekyll URL

Returns:

  • (String)

    PO page path (e.g., ‘docs/page.html/index.html’)



58
59
60
61
62
# File 'lib/jekyll-l10n/utils/url_path_builder.rb', line 58

def url_to_po_page_path(url)
  path = normalize_url(url)
  path = 'index' if path.empty?
  "#{path}/index.html"
end