Class: Uniword::Docx::JunkClassifier

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/docx/junk_classifier.rb

Overview

Classify a package-relative path as junk or legitimate.

Used by Docx::PartLoader::RawPartLoader to decide which unclaimed ZIP entries to carry as RawPart and which to strip. The classifier is the single source of truth for the junk decision — adding new criteria means changing this class (or its pattern constants), not the loader.

Classification is two-armed:

  1. OS/tooling artifact — path matches a known pattern (__MACOSX/, .DS_Store, Thumbs.db, ._*, ~$*). These are never legitimate document content; matched unconditionally even when a Default content type happens to apply.

  2. Undeclared part — no Override and no Default extension match in the loaded ContentTypes::Types, AND no modelled relationship targets the path. This is the OPC rule: every part must have a content type declaration.

Both arms return a human-readable reason string; the loader records it on Package#stripped_parts for caller introspection.

Examples:

classifier = JunkClassifier.new(
  content_types: package.content_types,
  relationships_by_path: rel_targets,
)
classifier.reason("[trash]/0000.dat")
# => "No content type declaration and no referencing relationship"
classifier.reason("word/document.xml") # => nil

Constant Summary collapse

OS_ARTIFACT_PATTERNS =

Path patterns for OS / tooling artifacts that are never legitimate document content. Append to extend; the classifier iterates them in order.

[
  /\A__MACOSX\//,         # macOS zip metadata directory
  /\A\.DS_Store\z/,       # macOS Finder
  /Thumbs\.db\z/,         # Windows Explorer
  /\A\._/,                # macOS AppleDouble resource fork
  /\A~\$/,                # Office lock files
].freeze
OS_ARTIFACT_REASON =
"OS or tooling artifact"
UNDECLARED_REASON =
"No content type declaration and no referencing relationship"

Instance Method Summary collapse

Constructor Details

#initialize(content_types:, relationships_by_path: nil) ⇒ JunkClassifier

Returns a new instance of JunkClassifier.

Parameters:

  • content_types (ContentTypes::Types, nil)

    the loaded content types model (nil when absent — every path is then undeclared unless a relationship targets it)

  • relationships_by_path (Hash{String => true}, nil) (defaults to: nil)

    set of paths targeted by some modelled relationship. Encoded as a hash for O(1) lookup; nil means "no relationships recorded"



59
60
61
62
# File 'lib/uniword/docx/junk_classifier.rb', line 59

def initialize(content_types:, relationships_by_path: nil)
  @content_types = content_types
  @relationships_by_path = relationships_by_path || {}
end

Instance Method Details

#junk?(path) ⇒ Boolean

Returns true when the path is junk.

Parameters:

  • path (String)

    package-relative path

Returns:

  • (Boolean)

    true when the path is junk



73
74
75
# File 'lib/uniword/docx/junk_classifier.rb', line 73

def junk?(path)
  !reason(path).nil?
end

#reason(path) ⇒ String?

Returns reason string when the path is junk; nil when it is legitimate.

Parameters:

  • path (String)

    package-relative path

Returns:

  • (String, nil)

    reason string when the path is junk; nil when it is legitimate



67
68
69
# File 'lib/uniword/docx/junk_classifier.rb', line 67

def reason(path)
  os_artifact_reason(path) || undeclared_reason(path)
end