forked from CollectionBuilder/collectionbuilder-csv
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix timeline * add error handling and parsing of unspecified dates * add more error handling * check for exmpty strings; fix assigning to wrong year if object has negative year * make coderabbit happy * keep coderabbit happy * improve error handling * fix date parsing (year "0000") * refactoring code * prettier * change sort order, remove period heading * fix regex * prettier * improve naming --------- Co-authored-by: Moritz Mähr <[email protected]>
- Loading branch information
Showing
3 changed files
with
113 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,50 @@ | ||
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) | ||
non_empty_dates = array_of_strings.reject { |str| str.strip.empty? } | ||
|
||
processed_dates = non_empty_dates.map do |str| | ||
normalized_date_str = replace_x_with_parsable_0(str) | ||
date_as_integer = normalize_to_integer(normalized_date_str) | ||
display_format = create_display_format(str) | ||
|
||
{ numeric: date_as_integer, original: str, display_format: display_format } | ||
end | ||
|
||
sorted_dates = processed_dates.sort_by { |date| -date[:numeric] } | ||
sorted_dates.map { |date| "#{date[:display_format]}:#{date[:original]}" } | ||
end | ||
|
||
private | ||
|
||
def replace_x_with_parsable_0(str) | ||
str.gsub(/[Xu]/, '0') | ||
end | ||
|
||
def normalize_to_integer(year_str) | ||
numeric_str = if year_str.start_with?('-') | ||
year_str.sub(/^-0+/, '-') | ||
else | ||
year_str.sub(/^0+(?=\d)/, '') | ||
end | ||
|
||
if numeric_str.match?(/^-?\d+$/) | ||
numeric_str.to_i | ||
else | ||
raise ArgumentError, "Invalid year format: #{year_str}" | ||
end | ||
end | ||
|
||
def create_display_format(str) | ||
if str.start_with?('-') | ||
"#{str[1..-1].sub(/^0+/, '')} v. u. Z." | ||
else | ||
str.sub(/^0+(?=\d)/, '') | ||
end | ||
end | ||
|
||
|
||
end | ||
end | ||
|
||
Liquid::Template.register_filter(Jekyll::SortEDTF) |