From db6d33eae1845e849f12faed6f5c0ff8d341fe89 Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Sat, 29 Feb 2020 21:47:59 -0500 Subject: [PATCH 1/2] Add sourcerer.io contributors --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 632e3bc..2a8a2de 100644 --- a/README.md +++ b/README.md @@ -103,3 +103,7 @@ Reader: Mobile UI: ![mobile screenshot](./.github/screenshots/mobile.png) + +## Contributors + +[![](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/images/0)](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/links/0)[![](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/images/1)](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/links/1)[![](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/images/2)](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/links/2)[![](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/images/3)](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/links/3)[![](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/images/4)](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/links/4)[![](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/images/5)](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/links/5)[![](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/images/6)](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/links/6)[![](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/images/7)](https://sourcerer.io/fame/hkalexling/hkalexling/Mango/links/7) From d1204335259079db56108a3603f620aa657c5e0b Mon Sep 17 00:00:00 2001 From: Leeingnyo Date: Fri, 13 Mar 2020 02:22:12 +0900 Subject: [PATCH 2/2] Sort page alphanumerically See https://ux.stackexchange.com/questions/95431/how-should-sorting-work-when-numeric-is-mixed-with-alpha-numeric --- src/library.cr | 3 ++- src/util.cr | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/library.cr b/src/library.cr index fcdfb58..634e1ea 100644 --- a/src/library.cr +++ b/src/library.cr @@ -2,6 +2,7 @@ require "zip" require "mime" require "json" require "uri" +require "./util" struct Image property data : Bytes @@ -44,7 +45,7 @@ class Entry ["image/jpeg", "image/png"].includes? \ MIME.from_filename? e.filename } - .sort { |a, b| a.filename <=> b.filename } + .sort { |a, b| compare_alphanumerically(split_by_alphanumeric(a.filename), split_by_alphanumeric(b.filename)) } .[page_num - 1] page.open do |io| slice = Bytes.new page.uncompressed_size diff --git a/src/util.cr b/src/util.cr index 343fbbf..86a3c2c 100644 --- a/src/util.cr +++ b/src/util.cr @@ -32,3 +32,50 @@ def request_path_startswith(env, ary) end return false end + +def is_numeric(str) + /^\d+/.match(str) != nil +end + +def split_by_alphanumeric(str) + is_number = false + arr = [] of String + arr.push( + str.split("").reduce("") do |memo, c| + if (is_number) + if is_numeric(c) + memo + c + else + arr.push(memo) if memo != "" + is_number = false + c + end + else + if is_numeric(c) # number + arr.push(memo) if memo != "" + is_number = true + c + else + memo + c + end + end + end + ) + arr +end + +def compare_alphanumerically(c, d) + is_c_bigger = c.size <=> d.size + begin + c.zip(d) do |a, b| + if is_numeric(a) && is_numeric(b) + compare = a.to_i <=> b.to_i + return compare if compare != 0 + else + compare = a <=> b + return compare if compare != 0 + end + end + is_c_bigger + end +end