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 1 commit
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
26 changes: 15 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 = '[-]?[\dXx]{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 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,33 @@
</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: ':' %}
<a class="dropdown-item" href="#y{{ year_parts[1] }}">{{ year_parts[0] }}</a>
koilebeit marked this conversation as resolved.
Show resolved Hide resolved
{% 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: ':' %}
<a href="#y{{ first_year[1] }}">{{ first_year[0] }}</a> &#8211;
<a href="#y{{ last_year[1]}}">{{ last_year[0] }}</a>
koilebeit marked this conversation as resolved.
Show resolved Hide resolved
</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 | where_exp: 'item', 'item[field] contains year[1]' -%}
koilebeit marked this conversation as resolved.
Show resolved Hide resolved
{% for item in inYear %}
<div class="col-lg-4 col-md-6">
<figure class="figure">
Expand Down
36 changes: 31 additions & 5 deletions _plugins/sort_edtf.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
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 }
end
module SortEDTF
def sort_edtf(array_of_strings)
# Parse each string into a hash with numeric, original, and display representations
parsed_dates = array_of_strings.map do |str|
cleaned_str = str.gsub(/[Xx]/, '0') # Replace X/x with 0 for numeric comparison
maehr marked this conversation as resolved.
Show resolved Hide resolved

# Remove leading zeros for numeric value calculation
numeric_value = if cleaned_str.start_with?('-')
cleaned_str.sub(/^-0+/, '-').to_i
else
cleaned_str.sub(/^0+/, '').to_i
end
koilebeit marked this conversation as resolved.
Show resolved Hide resolved

# 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] }
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)