Module: JekyllHighlightCards::DimensionParser
- Defined in:
- lib/jekyll-highlight-cards/dimension_parser.rb
Overview
Parse dimension specifications in "WxH" format
Supports multiple formats for specifying image dimensions:
WIDTHxHEIGHT: Both dimensions (e.g., "300x200")WIDTHx: Width only (e.g., "300x")xHEIGHT: Height only (e.g., "x200")WIDTH: Width only shorthand (e.g., "300")- Units: Supports px, em, %, etc. (e.g., "400pxx300px")
Class Method Summary collapse
-
.parse_dimensions(dim_str) ⇒ Array<String, nil>
Parse dimension string into width and height components.
Class Method Details
.parse_dimensions(dim_str) ⇒ Array<String, nil>
Parse dimension string into width and height components
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/jekyll-highlight-cards/dimension_parser.rb', line 27 def self.parse_dimensions(dim_str) return [nil, nil] if dim_str.nil? || dim_str.empty? # Determine the separator: # - If "xx" is present, the SECOND 'x' is the separator (first 'x' is part of the dimension) # Example: "400pxx300px" → width="400px", height="300px" # - Otherwise, check if there's an 'x' that's a separator (not part of a unit like "px") # An 'x' is a separator if it's at the end, followed by a digit, or at the start if dim_str.include?("xx") idx = dim_str.index("xx") width = dim_str[...(idx + 1)] height = dim_str[(idx + 2)..] height = nil if height.empty? [width, height] elsif dim_str.match?(/x\d|(?<![a-z])x\z/) || dim_str.start_with?("x") width_str, height_str = dim_str.split("x", 2) width = width_str width = nil if width.empty? height = height_str height = nil if height.empty? [width, height] else # No separator found - treat as width only [dim_str, nil] end end |