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
Class Method Summary collapse
-
.normalize_url(url) ⇒ String
Normalize a URL by removing leading and trailing slashes.
-
.relative_path(file_path, dest) ⇒ String
Calculate relative path from destination directory.
-
.url_to_file_path(url) ⇒ String
Convert a URL to a file system path.
-
.url_to_po_page_path(url) ⇒ String
Convert a URL to a PO page path for translation files.
Class Method Details
.normalize_url(url) ⇒ String
Normalize a URL by removing leading and trailing slashes.
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.
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.
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.
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 |