Module: CbzTools::ComicInfo
- Defined in:
- lib/cbz_tools/comic_info.rb,
sig/cbz_tools.rbs
Overview
Reads and writes the ComicInfo.xml metadata embedded in CBZ archives.
The two operations are symmetric over SCHEMA: every key on the left side of that hash is a field that ComicInfo.parse can read and ComicInfo.build can write. Extend the schema in place (for example, in an initializer) if you need to support custom fields.
Defined Under Namespace
Classes: ParseError
Constant Summary collapse
- SCHEMA =
{ title: "Title", series: "Series", series_sort: "SeriesSort", localized_series: "LocalizedSeries", number: "Number", count: "Count", volume: "Volume", summary: "Summary", notes: "Notes", publisher: "Publisher", imprint: "Imprint", year: "Year", month: "Month", day: "Day", writer: "Writer", penciller: "Penciller", inker: "Inker", colorist: "Colorist", letterer: "Letterer", cover_artist: "CoverArtist", editor: "Editor", translator: "Translator", genre: "Genre", tags: "Tags", web: "Web", page_count: "PageCount", language_iso: "LanguageISO", format: "Format", series_group: "SeriesGroup", age_rating: "AgeRating", community_rating: "CommunityRating", black_and_white: "BlackAndWhite", manga: "Manga", characters: "Characters", teams: "Teams", locations: "Locations", scan_information: "ScanInformation", story_arc: "StoryArc", gtin: "GTIN" }
Class Method Summary collapse
-
.build(metadata) ⇒ String
Builds a ComicInfo.xml string from a metadata hash.
-
.parse(xml) ⇒ Hash
Parses a ComicInfo.xml string into a symbol-keyed hash.
Class Method Details
.build(metadata) ⇒ String
Builds a ComicInfo.xml string from a metadata hash.
Accepts symbol or string keys; non-SCHEMA keys are ignored. Values
that are nil, empty, or whitespace-only are skipped so the
resulting XML does not contain empty tags.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cbz_tools/comic_info.rb', line 88 def self.build() normalized = .transform_keys(&:to_sym) builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml| xml.ComicInfo( "xmlns:xsd" => "http://www.w3.org/2001/XMLSchema", "xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance" ) do SCHEMA.each do |key, tag| value = normalized[key] next if value.nil? || value.to_s.strip.empty? xml.send(tag, value.to_s) end end end builder.to_xml end |
.parse(xml) ⇒ Hash
Parses a ComicInfo.xml string into a symbol-keyed hash.
Returns an empty hash when the input is empty, or when the XML is
valid but has no
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/cbz_tools/comic_info.rb', line 67 def self.parse(xml) doc = Nokogiri::XML(xml) raise ParseError, "Malformed XML: #{doc.errors.first&.}" if doc.errors.any? root = doc.at("ComicInfo") return {} unless root SCHEMA.each_with_object({}) do |(key, tag), hash| text = root.at(tag)&.text&.strip hash[key] = text if text && !text.empty? end end |