Last active
August 13, 2024 13:49
-
-
Save tomash/c9a8d8f87e008bbde5ccf3a813fb8ec8 to your computer and use it in GitHub Desktop.
Ebook renamer to "title - author.epub" (and associated mobi file if present)
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
# encoding: UTF-8 | |
# from the code dating back to 2009: https://github.com/tomash/ascii_tic | |
# Removes HTML tags from a string. Allows you to specify some tags to be kept. | |
module AsciiTic | |
def strip_html( allowed = [] ) | |
re = if allowed.any? | |
Regexp.new( | |
%(<(?!(\\s|\\/)*(#{ | |
allowed.map {|tag| Regexp.escape( tag )}.join( "|" ) | |
})( |>|\\/|'|"|<|\\s*\\z))[^>]*(>+|\\s*\\z)), | |
##' | |
Regexp::IGNORECASE | Regexp::MULTILINE, 'u' | |
) | |
else | |
/<[^>]*(>+|\s*\z)/m | |
end | |
gsub(re,'') | |
end | |
def capitalize_every_word | |
split(/\s+/).each{ |word| word.capitalize! }.join(' ') | |
end | |
def to_ascii_brutal | |
#foo = self.downcase.strip | |
foo = String.new(self) | |
#foo = self.clone #.downcase.strip | |
foo.gsub!(/[ĄÀ�?ÂÃ]/,'A') | |
foo.gsub!(/[âäàãáäå�?ăąǎǟǡǻ�?ȃȧẵặ]/,'a') | |
foo.gsub!(/[Ę]/,'E') | |
foo.gsub!(/[ëêéèẽēĕėẻȅȇẹȩęḙḛ�?ếễểḕḗệ�?]/,'e') | |
foo.gsub!(/[Ì�?ÎĨ]/,'I') | |
foo.gsub!(/[�?iìíîĩīĭïỉ�?ịįȉȋḭɨḯ]/,'i') | |
foo.gsub!(/[ÒÓÔÕÖ]/,'O') | |
foo.gsub!(/[òóôõ�?�?ȯö�?őǒ�?�?ơǫ�?ɵøồốỗổȱȫȭ�?�?ṑṓ�?ớỡởợǭộǿ]/,'o') | |
foo.gsub!(/[ÙÚÛŨÜ]/,'U') | |
foo.gsub!(/[ùúûũūŭüủůűǔȕȗưụṳųṷṵṹṻǖǜǘǖǚừứữửự]/,'u') | |
foo.gsub!(/[ỳýŷỹȳ�?ÿỷẙƴỵ]/,'y') | |
foo.gsub!(/[œ]/,'oe') | |
foo.gsub!(/[ÆǼǢæ]/,'ae') | |
foo.gsub!(/[Ń]/,'N') | |
foo.gsub!(/[ñǹń]/,'n') | |
foo.gsub!(/[ÇĆČ]/,'C') | |
foo.gsub!(/[çćč]/,'c') | |
foo.gsub!(/[ß]/,'ss') | |
foo.gsub!(/[œ]/,'oe') | |
foo.gsub!(/[ij]/,'ij') | |
foo.gsub!(/[Ł]/,'L') | |
foo.gsub!(/[�?ł]/,'l') | |
foo.gsub!(/[ŚŠ]/,'S') | |
foo.gsub!(/[śš]/,'s') | |
foo.gsub!(/[ŹŻ]/,'Z') | |
foo.gsub!(/[źż]/,'z') | |
#foo.sub!(/[\s\'\"\\\/\?\.\=\+\&\%]$/,'') | |
#foo.gsub!(/[\s\'\"\\\/\?\.\=\+\&\%]/,'_') | |
#foo.gsub!(/_+/,'_') | |
foo | |
end | |
def to_permalink(separator = '-') | |
permalink = self.to_ascii_brutal | |
#permalink = string.to_ascii_brutal | |
# all down | |
permalink.downcase! | |
# preserve alphanumerics, everything else becomes a separator | |
permalink.gsub!(/[^a-z0-9]+/, separator) | |
# enforce the maximum component length and return it | |
#permalink = permalink.to(max_size) | |
# trim any leading or trailing separators | |
return permalink.gsub(/^\#{separator}+|\#{separator}+$/, '') | |
end | |
end |
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
# frozen_string_literal: true | |
source "https://rubygems.org" | |
# https://kitaitimakoto.gitlab.io/epub-parser/file.Home.html | |
gem "epub-parser" |
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
#!/usr/bin/env ruby | |
require "fileutils" | |
require "epub/parser" | |
require_relative "ascii_tic" | |
String.prepend(AsciiTic) | |
epub_path = ARGV[0] | |
if(epub_path.nil? || !File.exist?(epub_path) || !File.file?(epub_path)) | |
abort "need valid path to epub file" | |
else | |
puts "#{epub_path} ..." | |
end | |
# 1. parse and extract metadata | |
book = EPUB::Parser.parse(epub_path) | |
# metadata: | |
md = book.package.metadata | |
title = md.titles[0].content | |
title_latin1 = title.to_ascii_brutal.strip | |
author = md.creators[0].content | |
author_latin1 = author.to_ascii_brutal.strip | |
if(title_latin1.empty?) | |
abort "...title in metadata empty, aborting." | |
end | |
if(author_latin1.empty?) | |
abort "...author in metadata empty, aborting." | |
end | |
# 2. rename the file | |
new_title = "#{title_latin1} - #{author_latin1}" | |
new_filename = "#{new_title}.epub" | |
if(File.basename(epub_path) != new_filename) | |
puts "...renaming to #{new_filename}" | |
new_epub_path = File.join(File.dirname(epub_path), "#{new_title}.epub") | |
FileUtils.mv(epub_path, new_epub_path) | |
else | |
puts "...file already named optimally in accordance with metadata" | |
end | |
# 3. check if there's associated mobi file, if yes then also rename | |
associated_mobi_name = File.basename(epub_path, File.extname(epub_path)) + ".mobi" | |
associated_mobi_path = File.join(File.dirname(epub_path), associated_mobi_name) | |
if(File.exist?(associated_mobi_path) && File.file?(associated_mobi_path)) | |
# mobi file found, let's rename it as well | |
puts "...found associated .mobi file" | |
if(File.basename(associated_mobi_name) != "#{new_title}.mobi") | |
puts "...renaming associated .mobi file as well" | |
new_mobi_path = File.join(File.dirname(associated_mobi_path), "#{new_title}.mobi") | |
FileUtils.mv(associated_mobi_path, new_mobi_path) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment