diff --git a/_layouts/timeline_edtf.html b/_layouts/timeline_edtf.html index 5a3efbc9..c1b19dc2 100644 --- a/_layouts/timeline_edtf.html +++ b/_layouts/timeline_edtf.html @@ -10,17 +10,16 @@ {% else %} {%- assign items = site.data[site.metadata] | where_exp: 'item', 'item.objectid and item.parentid == nil' -%} {% endif %} -{% assign regex = '[-]?[\dXu]{4,}' %} {%- assign items = items | where_exp: 'item', 'item[field]' -%} {%- assign raw-dates = items | map: field | compact | uniq -%} +{% assign regex = '[\dX]{4}' %} {%- capture clean-years -%}{% for date in raw-dates %}{{date | regex_match: regex | join: ';' }}{% unless forloop.last %};{% endunless %}{%- endfor -%}{%- endcapture -%} {%- assign uniqueYears = clean-years | remove: ' ' | replace: ';;', ';' | split: ';' | uniq | sort -%} -{% assign sorted_years = uniqueYears | sort_edtf %} {%- if site.data.theme['year-navigation'] -%} {%- assign navYears = site.data.theme['year-navigation'] | split: ';' -%} {%- elsif site.data.theme['year-nav-increment'] -%} {%- capture navYears -%} -{%- for i in sorted_years -%}{%- assign t = i | modulo: site.data.theme.year-nav-increment -%} +{%- for i in uniqueYears -%}{%- assign t = i | modulo: site.data.theme.year-nav-increment -%} {%- if t == 0 -%}{{ i }}{% unless forloop.last %};{% endunless %}{% endif %}{% endfor %}{%- endcapture -%} {%- assign navYears = navYears | split: ';' -%} {%- endif -%} @@ -38,13 +37,8 @@ {%- endif -%} @@ -52,25 +46,21 @@ {{ content }}

- {% assign first_year = sorted_years | first | split: ':' %} - {% assign last_year = sorted_years | last | split: ':' %} - {% if first_year.size == 2 and last_year.size == 2 %} - {{ first_year[0] }} – - {{ last_year[0] }} - {% endif %} + {{ uniqueYears | first }} + {{- uniqueYears | last -}} +

- {% for date in sorted_years %} - {% assign year = date | split: ':' %} - + {% for year in uniqueYears %} +
-

{{ year[0] }}

+

{{ year }}

- {%- assign inYear = items | filter_items_by_year: year[1], '[-]?[\dXu]{4,}' -%} + {%- assign inYear = items | where_exp: 'item', 'item[field] contains year' -%} {% for item in inYear %}
diff --git a/_plugins/jekyll_regex_match.rb b/_plugins/jekyll_regex_match.rb index b598cb00..24024758 100644 --- a/_plugins/jekyll_regex_match.rb +++ b/_plugins/jekyll_regex_match.rb @@ -20,36 +20,6 @@ def regex_match_once(input_str, regex_str) return nil end end - - def filter_items_by_year(items, year, regex_pattern = '[-]?[\dXu]{4,}') - raise ArgumentError, 'Regex pattern too complex' if regex_pattern.length > 50 - return [] unless items.is_a?(Array) && !year.nil? - begin - - # Clear cache if it grows too large - if defined?(@cache_size) && @cache_size > 1000 - items.each { |item| item.delete('cached_dates') } - @cache_size = 0 - end - - regex = Regexp.new(regex_pattern) - items.select do |item| - next false unless item.is_a?(Hash) && item['date'].is_a?(String) - - # Track cache size - @cache_size ||= 0 - @cache_size += 1 unless item['cached_dates'] - - dates = item['cached_dates'] ||= item['date'].scan(regex).map(&:to_s) - dates.include?(year) - end - rescue RegexpError => e - Jekyll.logger.error "RegexMatch:", "Invalid regex pattern: #{e.message}" - # Handle invalid regex pattern - [] - end - end - end end diff --git a/_plugins/sort_edtf.rb b/_plugins/sort_edtf.rb index e2e60737..b04774a1 100644 --- a/_plugins/sort_edtf.rb +++ b/_plugins/sort_edtf.rb @@ -1,47 +1,10 @@ module Jekyll - module SortEDTF - def sort_edtf(array_of_strings) - - # Remove any empty strings after stripping whitespace - valid_dates = array_of_strings.reject { |str| str.strip.empty? } - - # Parse each string into a hash with numeric, original, and display representations - parsed_dates = valid_dates.map do |str| - cleaned_str = str.gsub(/[Xu]/, '0') # Replace X/x with 0 for numeric comparison - - # Remove leading zeros for numeric value calculation - numeric_value = if cleaned_str.start_with?('-') - numeric_str = cleaned_str.sub(/^-0+/, '-') - else - numeric_str = cleaned_str.sub(/^0+/, '') - end - - # Validate that numeric_str is a valid integer - if numeric_str.match?(/^-?\d+$/) - numeric_value = numeric_str.to_i - else - raise ArgumentError, "Invalid date format: #{str}" + module SortEDTF + def sort_edtf(array_of_strings) + sorted = array_of_strings.map { |str| str.gsub('X', '0') } + sorted.sort_by { |str| str[/\d+/].to_i } end - - # Create a human-readable display format - # For display format, we use the original string, just without leading zeros - display_format = if str.start_with?('-') - "#{str[1..-1].sub(/^0+/, '')} v. Chr." # For negative, remove minus and leading zeros for display - else - str.sub(/^0+/, '') # Remove leading zeros for positive values - end - - # Return a hash with numeric, original, and display_format - { numeric: numeric_value, original: str, display_format: display_format } - end - - # Sort by the numeric representation - sorted_dates = parsed_dates.sort_by { |date| date[:numeric] } - - # Return an array of "display_format:original" strings - sorted_dates.map { |date| "#{date[:display_format]}:#{date[:original]}" } end - end end Liquid::Template.register_filter(Jekyll::SortEDTF) \ No newline at end of file