Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix timeline #171

Merged
merged 7 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions _layouts/timeline_edtf.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
{% else %}
{%- assign items = site.data[site.metadata] | where_exp: 'item', 'item.objectid and item.parentid == nil' -%}
{% endif %}
{% assign regex = '[-]?[\dXu]{4,}' %}
koilebeit marked this conversation as resolved.
Show resolved Hide resolved
{%- 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 %}
koilebeit marked this conversation as resolved.
Show resolved Hide resolved
{%- 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 uniqueYears -%}{%- assign t = i | modulo: site.data.theme.year-nav-increment -%}
{%- for i in sorted_years -%}{%- 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 -%}
Expand All @@ -37,30 +38,39 @@
</button>
<div class="dropdown-menu" aria-labelledby="yearButton">
{% for y in navYears %}
<a class="dropdown-item" href="#y{{ y }}">{{ y }}</a>
{%- endfor %}
{% assign year_parts = y | split: ':' %}
{% if year_parts.size == 2 %}
<a class="dropdown-item" href="#y{{ year_parts[1] }}">{{ year_parts[0] }}</a>
{% else %}
<a class="dropdown-item" href="#y{{ y }}">{{ y }}</a>
{% endif %}
{% endfor %}
</div>
</div>
{%- endif -%}

{{ content }}

<h2>
<a href="#y{{ uniqueYears | first }}">{{ uniqueYears | first }}</a>&#8211;<a href="#y{{ uniqueYears | last }}">
{{- uniqueYears | last -}}
</a>
{% assign first_year = sorted_years | first | split: ':' %}
{% assign last_year = sorted_years | last | split: ':' %}
{% if first_year.size == 2 and last_year.size == 2 %}
<a href="#y{{ first_year[1] }}">{{ first_year[0] }}</a> &#8211;
<a href="#y{{ last_year[1]}}">{{ last_year[0] }}</a>
{% endif %}
</h2>

<table id="timeline" class="table table-striped">
<tbody>
{% for year in uniqueYears %}
<tr id="y{{ year }}">
{% for date in sorted_years %}
{% assign year = date | split: ':' %}
<tr id="y{{ year[1] }}">
<th>
<h3>{{ year }}</h3>
<h3>{{ year[0] }}</h3>
</th>
<td>
<div class="row">
{%- assign inYear = items | where_exp: 'item', 'item[field] contains year' -%}
{%- assign inYear = items | filter_items_by_year: year[1], '[-]?[\dXu]{4,}' -%}
{% for item in inYear %}
<div class="col-lg-4 col-md-6">
<figure class="figure">
Expand Down
9 changes: 9 additions & 0 deletions _plugins/jekyll_regex_match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ def regex_match_once(input_str, regex_str)
return nil
end
end

def filter_items_by_year(items, year, regex_pattern = '[-]?[\dXu]{4,}')
regex = Regexp.new(regex_pattern)
koilebeit marked this conversation as resolved.
Show resolved Hide resolved
items.select do |item|
dates = item['date'].scan(regex).map(&:to_s)
dates.include?(year)
koilebeit marked this conversation as resolved.
Show resolved Hide resolved
end
end
koilebeit marked this conversation as resolved.
Show resolved Hide resolved

end
end

Expand Down
45 changes: 41 additions & 4 deletions _plugins/sort_edtf.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,47 @@
module Jekyll
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 }
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}"
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
koilebeit marked this conversation as resolved.
Show resolved Hide resolved

# Sort by the numeric representation
sorted_dates = parsed_dates.sort_by { |date| date[:numeric] }
koilebeit marked this conversation as resolved.
Show resolved Hide resolved

# Return an array of "display_format:original" strings
sorted_dates.map { |date| "#{date[:display_format]}:#{date[:original]}" }
koilebeit marked this conversation as resolved.
Show resolved Hide resolved
end
end
end

Liquid::Template.register_filter(Jekyll::SortEDTF)