Implement styles #3
11
CLAUDE.md
11
CLAUDE.md
@@ -25,18 +25,23 @@ Ezdoc is a Ruby gem for creating .docx files using a DSL. The gem generates vali
|
|||||||
|
|
||||||
- **Document** (`lib/ezdoc/document.rb`): Entry point via `Document.create`. Includes the Builder module and maintains a collection of nodes.
|
- **Document** (`lib/ezdoc/document.rb`): Entry point via `Document.create`. Includes the Builder module and maintains a collection of nodes.
|
||||||
|
|
||||||
- **Builder** (`lib/ezdoc/builder.rb`): DSL methods (`p`, `text`, `b`, `i`, `u`, `ul`, `ol`, `li`, `table`, `tr`, `td`). Uses a format stack for nested formatting and target tracking for content placement.
|
- **Builder** (`lib/ezdoc/builder.rb`): DSL methods (`p`, `text`, `h1`-`h6`, `b`, `i`, `u`, `ul`, `ol`, `li`, `table`, `tr`, `td`, `image`). Uses a format stack for nested formatting and target tracking for content placement.
|
||||||
|
|
||||||
- **Nodes** (`lib/ezdoc/nodes/`): Document element representations (Paragraph, Run, List, ListItem, Table, TableRow, TableCell). All inherit from `Nodes::Base`.
|
- **Nodes** (`lib/ezdoc/nodes/`): Document element representations (Paragraph, Run, Image, List, ListItem, Table, TableRow, TableCell). All inherit from `Nodes::Base`.
|
||||||
|
|
||||||
|
- **Style** (`lib/ezdoc/style.rb`): Style definitions with text properties (bold, italic, color, size, font) and paragraph properties (align, indent, spacing).
|
||||||
|
|
||||||
- **Package** (`lib/ezdoc/package.rb`): Assembles the docx ZIP structure using rubyzip. Coordinates XML generation.
|
- **Package** (`lib/ezdoc/package.rb`): Assembles the docx ZIP structure using rubyzip. Coordinates XML generation.
|
||||||
|
|
||||||
- **XML generators** (`lib/ezdoc/xml/`): Generate OOXML-compliant XML:
|
- **XML generators** (`lib/ezdoc/xml/`): Generate OOXML-compliant XML:
|
||||||
- `DocumentXml`: Main content with paragraphs, lists, tables
|
- `DocumentXml`: Main content with paragraphs, lists, tables, images
|
||||||
|
- `StylesXml`: styles.xml with built-in and custom styles
|
||||||
- `ContentTypes`: [Content_Types].xml
|
- `ContentTypes`: [Content_Types].xml
|
||||||
- `Relationships`: .rels files
|
- `Relationships`: .rels files
|
||||||
- `Numbering`: numbering.xml for lists
|
- `Numbering`: numbering.xml for lists
|
||||||
|
|
||||||
|
- **ImageDimensions** (`lib/ezdoc/image_dimensions.rb`): Uses fastimage gem to read image dimensions for EMU calculations.
|
||||||
|
|
||||||
### Data Flow
|
### Data Flow
|
||||||
|
|
||||||
1. User calls DSL methods on Document
|
1. User calls DSL methods on Document
|
||||||
|
|||||||
95
README.md
95
README.md
@@ -69,6 +69,93 @@ Ezdoc::Document.create("output.docx") do |doc|
|
|||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Headings
|
||||||
|
|
||||||
|
Use `h1` through `h6` for document headings:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
Ezdoc::Document.create("output.docx") do |doc|
|
||||||
|
doc.h1 "Document Title"
|
||||||
|
doc.h2 "Chapter 1"
|
||||||
|
doc.h3 "Section 1.1"
|
||||||
|
doc.h4 "Subsection"
|
||||||
|
doc.h5 "Minor heading"
|
||||||
|
doc.h6 "Smallest heading"
|
||||||
|
|
||||||
|
# Headings with formatted content
|
||||||
|
doc.h2 do
|
||||||
|
doc.text "Chapter with "
|
||||||
|
doc.i { doc.text "emphasis" }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
### Styles
|
||||||
|
|
||||||
|
Ezdoc includes built-in styles and supports custom style definitions.
|
||||||
|
|
||||||
|
#### Built-in Styles
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
Ezdoc::Document.create("output.docx") do |doc|
|
||||||
|
doc.p "This is a title", style: :title
|
||||||
|
doc.p "A subtitle", style: :subtitle
|
||||||
|
doc.p "A quotation", style: :quote
|
||||||
|
doc.p "puts 'code'", style: :code
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Custom Styles
|
||||||
|
|
||||||
|
Define your own styles with text and paragraph properties:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
Ezdoc::Document.create("output.docx") do |doc|
|
||||||
|
# Define custom styles
|
||||||
|
doc.define_style :warning,
|
||||||
|
bold: true,
|
||||||
|
color: "FF0000",
|
||||||
|
size: 14
|
||||||
|
|
||||||
|
doc.define_style :note,
|
||||||
|
italic: true,
|
||||||
|
color: "0066CC",
|
||||||
|
font: "Georgia"
|
||||||
|
|
||||||
|
doc.define_style :centered,
|
||||||
|
align: :center,
|
||||||
|
size: 12
|
||||||
|
|
||||||
|
# Apply to paragraphs
|
||||||
|
doc.p "Warning message!", style: :warning
|
||||||
|
doc.p "Centered text", style: :centered
|
||||||
|
|
||||||
|
# Apply to text runs
|
||||||
|
doc.p do
|
||||||
|
doc.text "Normal text, "
|
||||||
|
doc.text "important!", style: :warning
|
||||||
|
doc.text ", and "
|
||||||
|
doc.text "a note", style: :note
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Style Properties
|
||||||
|
|
||||||
|
**Text properties:**
|
||||||
|
- `bold: true/false`
|
||||||
|
- `italic: true/false`
|
||||||
|
- `underline: true/false`
|
||||||
|
- `color: "FF0000"` (hex RGB)
|
||||||
|
- `size: 14` (points)
|
||||||
|
- `font: "Arial"` (font family)
|
||||||
|
|
||||||
|
**Paragraph properties:**
|
||||||
|
- `align: :left / :center / :right / :justify`
|
||||||
|
- `indent: 720` (twips, 1 inch = 1440 twips)
|
||||||
|
- `spacing_before: 240` (twips)
|
||||||
|
- `spacing_after: 240` (twips)
|
||||||
|
|
||||||
### Lists
|
### Lists
|
||||||
|
|
||||||
#### Bullet Lists
|
#### Bullet Lists
|
||||||
@@ -202,12 +289,14 @@ end
|
|||||||
|
|
||||||
| Method | Description |
|
| Method | Description |
|
||||||
|--------|-------------|
|
|--------|-------------|
|
||||||
| `p(text)` | Create a paragraph with text |
|
| `p(text, style:)` | Create a paragraph with text and optional style |
|
||||||
| `p { }` | Create a paragraph with block content |
|
| `p(style:) { }` | Create a paragraph with block content and optional style |
|
||||||
| `text(value)` | Add text to the current context |
|
| `text(value, style:)` | Add text with optional style to the current context |
|
||||||
|
| `h1(text)` - `h6(text)` | Create headings (level 1-6) |
|
||||||
| `b { }` | Bold formatting |
|
| `b { }` | Bold formatting |
|
||||||
| `i { }` | Italic formatting |
|
| `i { }` | Italic formatting |
|
||||||
| `u { }` | Underline formatting |
|
| `u { }` | Underline formatting |
|
||||||
|
| `define_style(name, **props)` | Define a custom style |
|
||||||
| `ul { }` | Bullet list |
|
| `ul { }` | Bullet list |
|
||||||
| `ol { }` | Numbered list |
|
| `ol { }` | Numbered list |
|
||||||
| `li(text)` | List item with text |
|
| `li(text)` | List item with text |
|
||||||
|
|||||||
182
examples/full_demo.rb
Executable file
182
examples/full_demo.rb
Executable file
@@ -0,0 +1,182 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Full demo of all Ezdoc features
|
||||||
|
# Run with: bundle exec ruby examples/full_demo.rb
|
||||||
|
|
||||||
|
require_relative "../lib/ezdoc"
|
||||||
|
|
||||||
|
OUTPUT_FILE = File.expand_path("../example.docx", __dir__)
|
||||||
|
FIXTURES_DIR = File.expand_path("../test/fixtures", __dir__)
|
||||||
|
|
||||||
|
Ezdoc::Document.create(OUTPUT_FILE) do |doc|
|
||||||
|
# ============================================================================
|
||||||
|
# Custom Styles
|
||||||
|
# ============================================================================
|
||||||
|
doc.define_style :highlight, bold: true, color: "FF6600"
|
||||||
|
doc.define_style :success, color: "228B22", italic: true
|
||||||
|
doc.define_style :centered_large, align: :center, size: 16, bold: true
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# Title and Introduction
|
||||||
|
# ============================================================================
|
||||||
|
doc.h1 "Ezdoc Feature Demo"
|
||||||
|
doc.p "A comprehensive example of all supported features", style: :subtitle
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# 1. Text Formatting
|
||||||
|
# ============================================================================
|
||||||
|
doc.h2 "1. Text Formatting"
|
||||||
|
doc.p do
|
||||||
|
doc.text "This paragraph demonstrates "
|
||||||
|
doc.b { doc.text "bold" }
|
||||||
|
doc.text ", "
|
||||||
|
doc.i { doc.text "italic" }
|
||||||
|
doc.text ", "
|
||||||
|
doc.u { doc.text "underlined" }
|
||||||
|
doc.text ", and "
|
||||||
|
doc.b do
|
||||||
|
doc.i do
|
||||||
|
doc.u { doc.text "combined" }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
doc.text " formatting."
|
||||||
|
end
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# 2. Headings
|
||||||
|
# ============================================================================
|
||||||
|
doc.h2 "2. Headings"
|
||||||
|
doc.h3 "This is Heading 3"
|
||||||
|
doc.h4 "This is Heading 4"
|
||||||
|
doc.h5 "This is Heading 5"
|
||||||
|
doc.h6 "This is Heading 6"
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# 3. Built-in Styles
|
||||||
|
# ============================================================================
|
||||||
|
doc.h2 "3. Built-in Styles"
|
||||||
|
doc.p "This is styled as a title", style: :title
|
||||||
|
doc.p "This is styled as a subtitle", style: :subtitle
|
||||||
|
doc.p "This is styled as a quote - perfect for citations and quotations.", style: :quote
|
||||||
|
doc.p "def hello; puts \"world\"; end", style: :code
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# 4. Custom Styles
|
||||||
|
# ============================================================================
|
||||||
|
doc.h2 "4. Custom Styles"
|
||||||
|
doc.p "This text uses our custom highlight style!", style: :highlight
|
||||||
|
doc.p do
|
||||||
|
doc.text "Mixed styles: "
|
||||||
|
doc.text "success message", style: :success
|
||||||
|
doc.text " and "
|
||||||
|
doc.text "highlighted text", style: :highlight
|
||||||
|
doc.text " in one paragraph."
|
||||||
|
end
|
||||||
|
doc.p "Centered and large text", style: :centered_large
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# 5. Lists
|
||||||
|
# ============================================================================
|
||||||
|
doc.h2 "5. Lists"
|
||||||
|
|
||||||
|
doc.h3 "Bullet List"
|
||||||
|
doc.ul do
|
||||||
|
doc.li "First item"
|
||||||
|
doc.li "Second item"
|
||||||
|
doc.li do
|
||||||
|
doc.text "Item with "
|
||||||
|
doc.b { doc.text "bold" }
|
||||||
|
doc.text " text"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
doc.h3 "Numbered List"
|
||||||
|
doc.ol do
|
||||||
|
doc.li "Step one"
|
||||||
|
doc.li "Step two"
|
||||||
|
doc.li "Step three"
|
||||||
|
end
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# 6. Tables
|
||||||
|
# ============================================================================
|
||||||
|
doc.h2 "6. Tables"
|
||||||
|
doc.table do
|
||||||
|
doc.tr do
|
||||||
|
doc.td { doc.b { doc.text "Feature" } }
|
||||||
|
doc.td { doc.b { doc.text "Status" } }
|
||||||
|
doc.td { doc.b { doc.text "Notes" } }
|
||||||
|
end
|
||||||
|
doc.tr do
|
||||||
|
doc.td "Paragraphs"
|
||||||
|
doc.td { doc.text "Complete", style: :success }
|
||||||
|
doc.td "Basic text support"
|
||||||
|
end
|
||||||
|
doc.tr do
|
||||||
|
doc.td "Formatting"
|
||||||
|
doc.td { doc.text "Complete", style: :success }
|
||||||
|
doc.td "Bold, italic, underline"
|
||||||
|
end
|
||||||
|
doc.tr do
|
||||||
|
doc.td "Headings"
|
||||||
|
doc.td { doc.text "Complete", style: :success }
|
||||||
|
doc.td "h1 through h6"
|
||||||
|
end
|
||||||
|
doc.tr do
|
||||||
|
doc.td "Styles"
|
||||||
|
doc.td { doc.text "Complete", style: :success }
|
||||||
|
doc.td "Built-in and custom"
|
||||||
|
end
|
||||||
|
doc.tr do
|
||||||
|
doc.td "Images"
|
||||||
|
doc.td { doc.text "Complete", style: :success }
|
||||||
|
doc.td "PNG and JPEG"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# 7. Images
|
||||||
|
# ============================================================================
|
||||||
|
doc.h2 "7. Images"
|
||||||
|
|
||||||
|
doc.p "Image with explicit dimensions:"
|
||||||
|
doc.p do
|
||||||
|
doc.image File.join(FIXTURES_DIR, "test.png"), width: "2in", height: "2in"
|
||||||
|
end
|
||||||
|
|
||||||
|
doc.p "Inline image with text:"
|
||||||
|
doc.p do
|
||||||
|
doc.text "Before "
|
||||||
|
doc.image File.join(FIXTURES_DIR, "test.jpg"), width: "0.75in", height: "0.75in"
|
||||||
|
doc.text " After"
|
||||||
|
end
|
||||||
|
|
||||||
|
doc.p "Image in a table:"
|
||||||
|
doc.table do
|
||||||
|
doc.tr do
|
||||||
|
doc.td "Description"
|
||||||
|
doc.td do
|
||||||
|
doc.image File.join(FIXTURES_DIR, "test.png"), width: "1in", height: "1in"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# ============================================================================
|
||||||
|
# 8. Combined Features
|
||||||
|
# ============================================================================
|
||||||
|
doc.h2 "8. Combined Features"
|
||||||
|
doc.p do
|
||||||
|
doc.text "This final paragraph combines "
|
||||||
|
doc.b { doc.text "multiple" }
|
||||||
|
doc.text " "
|
||||||
|
doc.i { doc.text "formatting" }
|
||||||
|
doc.text " options with "
|
||||||
|
doc.text "custom styles", style: :highlight
|
||||||
|
doc.text " to demonstrate the full power of Ezdoc."
|
||||||
|
end
|
||||||
|
|
||||||
|
doc.p "End of demo document.", style: :centered_large
|
||||||
|
end
|
||||||
|
|
||||||
|
puts "Created #{OUTPUT_FILE}"
|
||||||
@@ -13,10 +13,12 @@ require_relative "ezdoc/nodes/table"
|
|||||||
require_relative "ezdoc/nodes/table_row"
|
require_relative "ezdoc/nodes/table_row"
|
||||||
require_relative "ezdoc/nodes/table_cell"
|
require_relative "ezdoc/nodes/table_cell"
|
||||||
require_relative "ezdoc/image_dimensions"
|
require_relative "ezdoc/image_dimensions"
|
||||||
|
require_relative "ezdoc/style"
|
||||||
require_relative "ezdoc/xml/content_types"
|
require_relative "ezdoc/xml/content_types"
|
||||||
require_relative "ezdoc/xml/relationships"
|
require_relative "ezdoc/xml/relationships"
|
||||||
require_relative "ezdoc/xml/document_xml"
|
require_relative "ezdoc/xml/document_xml"
|
||||||
require_relative "ezdoc/xml/numbering"
|
require_relative "ezdoc/xml/numbering"
|
||||||
|
require_relative "ezdoc/xml/styles_xml"
|
||||||
require_relative "ezdoc/builder"
|
require_relative "ezdoc/builder"
|
||||||
require_relative "ezdoc/package"
|
require_relative "ezdoc/package"
|
||||||
require_relative "ezdoc/document"
|
require_relative "ezdoc/document"
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
module Ezdoc
|
module Ezdoc
|
||||||
module Builder
|
module Builder
|
||||||
def p(text = nil, &block)
|
def p(text = nil, style: nil, &block)
|
||||||
para = Nodes::Paragraph.new
|
para = Nodes::Paragraph.new(style: resolve_style(style))
|
||||||
if block
|
if block
|
||||||
with_target(para, &block)
|
with_target(para, &block)
|
||||||
elsif text
|
elsif text
|
||||||
@@ -12,8 +12,34 @@ module Ezdoc
|
|||||||
@nodes << para
|
@nodes << para
|
||||||
end
|
end
|
||||||
|
|
||||||
def text(value)
|
def text(value, style: nil)
|
||||||
@current_target.add_run(Nodes::Run.new(value, **current_formatting))
|
formatting = current_formatting.merge(style: resolve_style(style))
|
||||||
|
@current_target.add_run(Nodes::Run.new(value, **formatting))
|
||||||
|
end
|
||||||
|
|
||||||
|
# Heading shortcuts
|
||||||
|
def h1(text = nil, &block)
|
||||||
|
p(text, style: :heading1, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
def h2(text = nil, &block)
|
||||||
|
p(text, style: :heading2, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
def h3(text = nil, &block)
|
||||||
|
p(text, style: :heading3, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
def h4(text = nil, &block)
|
||||||
|
p(text, style: :heading4, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
def h5(text = nil, &block)
|
||||||
|
p(text, style: :heading5, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
def h6(text = nil, &block)
|
||||||
|
p(text, style: :heading6, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def image(path, width: nil, height: nil)
|
def image(path, width: nil, height: nil)
|
||||||
@@ -125,5 +151,12 @@ module Ezdoc
|
|||||||
|
|
||||||
raise ArgumentError, "Unsupported image format: #{ext}. Use PNG or JPEG."
|
raise ArgumentError, "Unsupported image format: #{ext}. Use PNG or JPEG."
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def resolve_style(style_or_name)
|
||||||
|
return nil if style_or_name.nil?
|
||||||
|
return style_or_name if style_or_name.is_a?(Style)
|
||||||
|
|
||||||
|
style(style_or_name) || raise(ArgumentError, "Unknown style: #{style_or_name}")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ module Ezdoc
|
|||||||
class Document
|
class Document
|
||||||
include Builder
|
include Builder
|
||||||
|
|
||||||
attr_reader :nodes
|
attr_reader :nodes, :styles
|
||||||
|
|
||||||
def self.create(path, &block)
|
def self.create(path, &block)
|
||||||
doc = new
|
doc = new
|
||||||
@@ -22,6 +22,16 @@ module Ezdoc
|
|||||||
@current_row = nil
|
@current_row = nil
|
||||||
@num_id_counter = 0
|
@num_id_counter = 0
|
||||||
@images = {}
|
@images = {}
|
||||||
|
@styles = {}
|
||||||
|
register_built_in_styles
|
||||||
|
end
|
||||||
|
|
||||||
|
def define_style(name, **properties)
|
||||||
|
@styles[name] = Style.new(name, **properties)
|
||||||
|
end
|
||||||
|
|
||||||
|
def style(name)
|
||||||
|
@styles[name]
|
||||||
end
|
end
|
||||||
|
|
||||||
def save(path)
|
def save(path)
|
||||||
@@ -49,8 +59,27 @@ module Ezdoc
|
|||||||
private
|
private
|
||||||
|
|
||||||
def next_image_rid
|
def next_image_rid
|
||||||
base = lists.any? ? 2 : 1
|
# rId1 = styles.xml (always present)
|
||||||
|
# rId2 = numbering.xml (if lists present)
|
||||||
|
# rId3+ = images
|
||||||
|
base = lists.any? ? 3 : 2
|
||||||
"rId#{base + @images.size}"
|
"rId#{base + @images.size}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def register_built_in_styles
|
||||||
|
# Headings
|
||||||
|
define_style :heading1, size: 24, bold: true
|
||||||
|
define_style :heading2, size: 18, bold: true
|
||||||
|
define_style :heading3, size: 14, bold: true
|
||||||
|
define_style :heading4, size: 12, bold: true
|
||||||
|
define_style :heading5, size: 11, bold: true, italic: true
|
||||||
|
define_style :heading6, size: 10, bold: true, italic: true
|
||||||
|
|
||||||
|
# Other built-in styles
|
||||||
|
define_style :title, size: 26, bold: true, align: :center
|
||||||
|
define_style :subtitle, size: 15, italic: true, color: "666666"
|
||||||
|
define_style :quote, italic: true, color: "666666", indent: 720
|
||||||
|
define_style :code, font: "Courier New", size: 10
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -3,11 +3,12 @@
|
|||||||
module Ezdoc
|
module Ezdoc
|
||||||
module Nodes
|
module Nodes
|
||||||
class Paragraph < Base
|
class Paragraph < Base
|
||||||
attr_reader :runs
|
attr_reader :runs, :style
|
||||||
|
|
||||||
def initialize(runs = [])
|
def initialize(runs = [], style: nil)
|
||||||
super()
|
super()
|
||||||
@runs = runs
|
@runs = runs
|
||||||
|
@style = style
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_run(run)
|
def add_run(run)
|
||||||
|
|||||||
@@ -3,14 +3,15 @@
|
|||||||
module Ezdoc
|
module Ezdoc
|
||||||
module Nodes
|
module Nodes
|
||||||
class Run < Base
|
class Run < Base
|
||||||
attr_reader :text, :bold, :italic, :underline
|
attr_reader :text, :bold, :italic, :underline, :style
|
||||||
|
|
||||||
def initialize(text, bold: false, italic: false, underline: false)
|
def initialize(text, bold: false, italic: false, underline: false, style: nil)
|
||||||
super()
|
super()
|
||||||
@text = text
|
@text = text
|
||||||
@bold = bold
|
@bold = bold
|
||||||
@italic = italic
|
@italic = italic
|
||||||
@underline = underline
|
@underline = underline
|
||||||
|
@style = style
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ module Ezdoc
|
|||||||
zipfile.get_output_stream("_rels/.rels") { |f| f.write(relationships_xml) }
|
zipfile.get_output_stream("_rels/.rels") { |f| f.write(relationships_xml) }
|
||||||
zipfile.get_output_stream("word/_rels/document.xml.rels") { |f| f.write(document_relationships_xml) }
|
zipfile.get_output_stream("word/_rels/document.xml.rels") { |f| f.write(document_relationships_xml) }
|
||||||
zipfile.get_output_stream("word/document.xml") { |f| f.write(document_xml) }
|
zipfile.get_output_stream("word/document.xml") { |f| f.write(document_xml) }
|
||||||
|
zipfile.get_output_stream("word/styles.xml") { |f| f.write(styles_xml) }
|
||||||
|
|
||||||
zipfile.get_output_stream("word/numbering.xml") { |f| f.write(numbering_xml) } if lists?
|
zipfile.get_output_stream("word/numbering.xml") { |f| f.write(numbering_xml) } if lists?
|
||||||
|
|
||||||
@@ -36,7 +37,7 @@ module Ezdoc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def content_types_xml
|
def content_types_xml
|
||||||
Xml::ContentTypes.new(has_numbering: lists?, images: images).to_xml
|
Xml::ContentTypes.new(has_numbering: lists?, images: images, has_styles: true).to_xml
|
||||||
end
|
end
|
||||||
|
|
||||||
def relationships_xml
|
def relationships_xml
|
||||||
@@ -44,13 +45,17 @@ module Ezdoc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def document_relationships_xml
|
def document_relationships_xml
|
||||||
Xml::DocumentRelationships.new(has_numbering: lists?, images: images).to_xml
|
Xml::DocumentRelationships.new(has_numbering: lists?, images: images, has_styles: true).to_xml
|
||||||
end
|
end
|
||||||
|
|
||||||
def document_xml
|
def document_xml
|
||||||
Xml::DocumentXml.new(@document.nodes).to_xml
|
Xml::DocumentXml.new(@document.nodes).to_xml
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def styles_xml
|
||||||
|
Xml::StylesXml.new(@document.styles).to_xml
|
||||||
|
end
|
||||||
|
|
||||||
def numbering_xml
|
def numbering_xml
|
||||||
Xml::Numbering.new(@document.lists).to_xml
|
Xml::Numbering.new(@document.lists).to_xml
|
||||||
end
|
end
|
||||||
|
|||||||
65
lib/ezdoc/style.rb
Normal file
65
lib/ezdoc/style.rb
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Ezdoc
|
||||||
|
class Style
|
||||||
|
attr_reader :name, :bold, :italic, :underline, :color, :size, :font,
|
||||||
|
:align, :indent, :spacing_before, :spacing_after
|
||||||
|
|
||||||
|
ALIGNMENTS = %i[left center right justify].freeze
|
||||||
|
|
||||||
|
def initialize(name, bold: nil, italic: nil, underline: nil, color: nil,
|
||||||
|
size: nil, font: nil, align: nil, indent: nil,
|
||||||
|
spacing_before: nil, spacing_after: nil)
|
||||||
|
@name = name
|
||||||
|
@bold = bold
|
||||||
|
@italic = italic
|
||||||
|
@underline = underline
|
||||||
|
@color = normalize_color(color)
|
||||||
|
@size = size
|
||||||
|
@font = font
|
||||||
|
@align = validate_align(align)
|
||||||
|
@indent = indent
|
||||||
|
@spacing_before = spacing_before
|
||||||
|
@spacing_after = spacing_after
|
||||||
|
end
|
||||||
|
|
||||||
|
def style_id
|
||||||
|
name.to_s.split("_").map(&:capitalize).join
|
||||||
|
end
|
||||||
|
|
||||||
|
def display_name
|
||||||
|
name.to_s.split("_").map(&:capitalize).join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
def paragraph_properties?
|
||||||
|
!!(align || indent || spacing_before || spacing_after)
|
||||||
|
end
|
||||||
|
|
||||||
|
def text_properties?
|
||||||
|
!!(bold || italic || underline || color || size || font)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Size in half-points for OOXML (14pt = 28 half-points)
|
||||||
|
def size_half_points
|
||||||
|
size ? (size * 2).to_i : nil
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def normalize_color(color)
|
||||||
|
return nil if color.nil?
|
||||||
|
|
||||||
|
hex = color.to_s.sub(/^#/, "").upcase
|
||||||
|
return hex if hex.match?(/\A[0-9A-F]{6}\z/)
|
||||||
|
|
||||||
|
raise ArgumentError, "Invalid color: #{color}. Use 6-digit hex (e.g., 'FF0000')"
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_align(align)
|
||||||
|
return nil if align.nil?
|
||||||
|
return align if ALIGNMENTS.include?(align)
|
||||||
|
|
||||||
|
raise ArgumentError, "Invalid alignment: #{align}. Use #{ALIGNMENTS.join(", ")}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -5,9 +5,10 @@ module Ezdoc
|
|||||||
class ContentTypes
|
class ContentTypes
|
||||||
NAMESPACE = "http://schemas.openxmlformats.org/package/2006/content-types"
|
NAMESPACE = "http://schemas.openxmlformats.org/package/2006/content-types"
|
||||||
|
|
||||||
def initialize(has_numbering: false, images: [])
|
def initialize(has_numbering: false, images: [], has_styles: false)
|
||||||
@has_numbering = has_numbering
|
@has_numbering = has_numbering
|
||||||
@images = images
|
@images = images
|
||||||
|
@has_styles = has_styles
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_xml
|
def to_xml
|
||||||
@@ -24,6 +25,12 @@ module Ezdoc
|
|||||||
PartName: "/word/document.xml",
|
PartName: "/word/document.xml",
|
||||||
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"
|
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"
|
||||||
)
|
)
|
||||||
|
if @has_styles
|
||||||
|
xml.Override(
|
||||||
|
PartName: "/word/styles.xml",
|
||||||
|
ContentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"
|
||||||
|
)
|
||||||
|
end
|
||||||
if @has_numbering
|
if @has_numbering
|
||||||
xml.Override(
|
xml.Override(
|
||||||
PartName: "/word/numbering.xml",
|
PartName: "/word/numbering.xml",
|
||||||
|
|||||||
@@ -42,6 +42,11 @@ module Ezdoc
|
|||||||
|
|
||||||
def render_paragraph(xml, para)
|
def render_paragraph(xml, para)
|
||||||
xml["w"].p do
|
xml["w"].p do
|
||||||
|
if para.style
|
||||||
|
xml["w"].pPr do
|
||||||
|
xml["w"].pStyle("w:val" => para.style.style_id)
|
||||||
|
end
|
||||||
|
end
|
||||||
para.runs.each { |run| render_run(xml, run) }
|
para.runs.each { |run| render_run(xml, run) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -73,8 +78,9 @@ module Ezdoc
|
|||||||
|
|
||||||
def render_text_run(xml, run)
|
def render_text_run(xml, run)
|
||||||
xml["w"].r do
|
xml["w"].r do
|
||||||
if run.bold || run.italic || run.underline
|
if run.bold || run.italic || run.underline || run.style
|
||||||
xml["w"].rPr do
|
xml["w"].rPr do
|
||||||
|
xml["w"].rStyle("w:val" => run.style.style_id) if run.style
|
||||||
xml["w"].b if run.bold
|
xml["w"].b if run.bold
|
||||||
xml["w"].i if run.italic
|
xml["w"].i if run.italic
|
||||||
xml["w"].u("w:val" => "single") if run.underline
|
xml["w"].u("w:val" => "single") if run.underline
|
||||||
|
|||||||
@@ -21,24 +21,38 @@ module Ezdoc
|
|||||||
|
|
||||||
class DocumentRelationships
|
class DocumentRelationships
|
||||||
NAMESPACE = "http://schemas.openxmlformats.org/package/2006/relationships"
|
NAMESPACE = "http://schemas.openxmlformats.org/package/2006/relationships"
|
||||||
|
STYLES_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
|
||||||
|
NUMBERING_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering"
|
||||||
IMAGE_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
|
IMAGE_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
|
||||||
|
|
||||||
def initialize(has_numbering: false, images: [])
|
def initialize(has_numbering: false, images: [], has_styles: false)
|
||||||
@has_numbering = has_numbering
|
@has_numbering = has_numbering
|
||||||
@images = images
|
@images = images
|
||||||
|
@has_styles = has_styles
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_xml
|
def to_xml
|
||||||
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
||||||
xml.Relationships(xmlns: NAMESPACE) do
|
xml.Relationships(xmlns: NAMESPACE) do
|
||||||
if @has_numbering
|
# rId1 = styles.xml (always first when present)
|
||||||
|
if @has_styles
|
||||||
xml.Relationship(
|
xml.Relationship(
|
||||||
Id: "rId1",
|
Id: "rId1",
|
||||||
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering",
|
Type: STYLES_TYPE,
|
||||||
|
Target: "styles.xml"
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
# rId2 = numbering.xml (if lists present)
|
||||||
|
if @has_numbering
|
||||||
|
xml.Relationship(
|
||||||
|
Id: "rId2",
|
||||||
|
Type: NUMBERING_TYPE,
|
||||||
Target: "numbering.xml"
|
Target: "numbering.xml"
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Images start at rId2 or rId3 depending on numbering
|
||||||
@images.each do |image|
|
@images.each do |image|
|
||||||
xml.Relationship(
|
xml.Relationship(
|
||||||
Id: image.rid,
|
Id: image.rid,
|
||||||
|
|||||||
66
lib/ezdoc/xml/styles_xml.rb
Normal file
66
lib/ezdoc/xml/styles_xml.rb
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Ezdoc
|
||||||
|
module Xml
|
||||||
|
class StylesXml
|
||||||
|
NAMESPACE = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
||||||
|
|
||||||
|
ALIGNMENT_MAP = {
|
||||||
|
left: "left",
|
||||||
|
center: "center",
|
||||||
|
right: "right",
|
||||||
|
justify: "both"
|
||||||
|
}.freeze
|
||||||
|
|
||||||
|
def initialize(styles)
|
||||||
|
@styles = styles
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_xml
|
||||||
|
builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
||||||
|
xml.styles("xmlns:w" => NAMESPACE) do
|
||||||
|
xml.parent.namespace = xml.parent.namespace_definitions.find { |ns| ns.prefix == "w" }
|
||||||
|
|
||||||
|
@styles.each_value do |style|
|
||||||
|
render_style(xml, style)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
builder.to_xml
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def render_style(xml, style)
|
||||||
|
style_type = style.paragraph_properties? ? "paragraph" : "character"
|
||||||
|
|
||||||
|
xml["w"].style("w:type" => style_type, "w:styleId" => style.style_id) do
|
||||||
|
xml["w"].name("w:val" => style.display_name)
|
||||||
|
|
||||||
|
render_paragraph_properties(xml, style) if style.paragraph_properties?
|
||||||
|
render_run_properties(xml, style) if style.text_properties?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_paragraph_properties(xml, style)
|
||||||
|
xml["w"].pPr do
|
||||||
|
xml["w"].jc("w:val" => ALIGNMENT_MAP[style.align]) if style.align
|
||||||
|
xml["w"].ind("w:left" => style.indent.to_s) if style.indent
|
||||||
|
xml["w"].spacing("w:before" => style.spacing_before.to_s) if style.spacing_before
|
||||||
|
xml["w"].spacing("w:after" => style.spacing_after.to_s) if style.spacing_after
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_run_properties(xml, style)
|
||||||
|
xml["w"].rPr do
|
||||||
|
xml["w"].rFonts("w:ascii" => style.font, "w:hAnsi" => style.font) if style.font
|
||||||
|
xml["w"].sz("w:val" => style.size_half_points.to_s) if style.size
|
||||||
|
xml["w"].color("w:val" => style.color) if style.color
|
||||||
|
xml["w"].b if style.bold
|
||||||
|
xml["w"].i if style.italic
|
||||||
|
xml["w"].u("w:val" => "single") if style.underline
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
87
test/heading_test.rb
Normal file
87
test/heading_test.rb
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class HeadingTest < Minitest::Test
|
||||||
|
include EzdocTestHelpers
|
||||||
|
|
||||||
|
def test_h1
|
||||||
|
xml = create_doc_and_read_xml { |doc| doc.h1 "Title" }
|
||||||
|
|
||||||
|
assert_includes xml, "<w:pStyle"
|
||||||
|
assert_includes xml, 'w:val="Heading1"'
|
||||||
|
assert_includes xml, "Title"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_h2
|
||||||
|
xml = create_doc_and_read_xml { |doc| doc.h2 "Chapter" }
|
||||||
|
|
||||||
|
assert_includes xml, 'w:val="Heading2"'
|
||||||
|
assert_includes xml, "Chapter"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_h3
|
||||||
|
xml = create_doc_and_read_xml { |doc| doc.h3 "Section" }
|
||||||
|
|
||||||
|
assert_includes xml, 'w:val="Heading3"'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_h4
|
||||||
|
xml = create_doc_and_read_xml { |doc| doc.h4 "Subsection" }
|
||||||
|
|
||||||
|
assert_includes xml, 'w:val="Heading4"'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_h5
|
||||||
|
xml = create_doc_and_read_xml { |doc| doc.h5 "Minor" }
|
||||||
|
|
||||||
|
assert_includes xml, 'w:val="Heading5"'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_h6
|
||||||
|
xml = create_doc_and_read_xml { |doc| doc.h6 "Smallest" }
|
||||||
|
|
||||||
|
assert_includes xml, 'w:val="Heading6"'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_heading_with_block
|
||||||
|
xml = create_doc_and_read_xml do |doc|
|
||||||
|
doc.h1 do
|
||||||
|
doc.text "Part 1 "
|
||||||
|
doc.b { doc.text "Bold" }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_includes xml, 'w:val="Heading1"'
|
||||||
|
assert_includes xml, "Part 1 "
|
||||||
|
assert_includes xml, "<w:b/>"
|
||||||
|
assert_includes xml, "Bold"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_multiple_headings
|
||||||
|
xml = create_doc_and_read_xml do |doc|
|
||||||
|
doc.h1 "Title"
|
||||||
|
doc.h2 "Chapter 1"
|
||||||
|
doc.h3 "Section 1.1"
|
||||||
|
doc.p "Normal paragraph"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_includes xml, 'w:val="Heading1"'
|
||||||
|
assert_includes xml, 'w:val="Heading2"'
|
||||||
|
assert_includes xml, 'w:val="Heading3"'
|
||||||
|
# Regular paragraph should not have pStyle
|
||||||
|
assert_equal 3, xml.scan("<w:pStyle").count
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_heading_styles_in_styles_xml
|
||||||
|
xml_files = create_doc_and_read_all_xml do |doc|
|
||||||
|
doc.h1 "Test"
|
||||||
|
end
|
||||||
|
|
||||||
|
styles_xml = xml_files["word/styles.xml"]
|
||||||
|
assert styles_xml, "styles.xml should exist"
|
||||||
|
assert_includes styles_xml, 'w:styleId="Heading1"'
|
||||||
|
assert_includes styles_xml, "<w:b/>"
|
||||||
|
assert_includes styles_xml, "<w:sz"
|
||||||
|
end
|
||||||
|
end
|
||||||
171
test/style_test.rb
Normal file
171
test/style_test.rb
Normal file
@@ -0,0 +1,171 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "test_helper"
|
||||||
|
|
||||||
|
class StyleTest < Minitest::Test
|
||||||
|
include EzdocTestHelpers
|
||||||
|
|
||||||
|
def test_define_custom_style
|
||||||
|
xml_files = create_doc_and_read_all_xml do |doc|
|
||||||
|
doc.define_style :warning, bold: true, color: "FF0000"
|
||||||
|
doc.p "Test", style: :warning
|
||||||
|
end
|
||||||
|
|
||||||
|
styles_xml = xml_files["word/styles.xml"]
|
||||||
|
assert_includes styles_xml, 'w:styleId="Warning"'
|
||||||
|
assert_includes styles_xml, "<w:b/>"
|
||||||
|
assert_includes styles_xml, 'w:val="FF0000"'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_apply_style_to_paragraph
|
||||||
|
xml = create_doc_and_read_xml do |doc|
|
||||||
|
doc.p "Quote text", style: :quote
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_includes xml, '<w:pStyle w:val="Quote"'
|
||||||
|
assert_includes xml, "Quote text"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_apply_style_to_text_run
|
||||||
|
xml = create_doc_and_read_xml do |doc|
|
||||||
|
doc.p do
|
||||||
|
doc.text "Normal "
|
||||||
|
doc.text "code", style: :code
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_includes xml, '<w:rStyle w:val="Code"'
|
||||||
|
assert_includes xml, "code"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_built_in_styles_exist
|
||||||
|
xml_files = create_doc_and_read_all_xml do |doc|
|
||||||
|
doc.p "Test"
|
||||||
|
end
|
||||||
|
|
||||||
|
styles_xml = xml_files["word/styles.xml"]
|
||||||
|
assert_includes styles_xml, 'w:styleId="Heading1"'
|
||||||
|
assert_includes styles_xml, 'w:styleId="Heading2"'
|
||||||
|
assert_includes styles_xml, 'w:styleId="Title"'
|
||||||
|
assert_includes styles_xml, 'w:styleId="Subtitle"'
|
||||||
|
assert_includes styles_xml, 'w:styleId="Quote"'
|
||||||
|
assert_includes styles_xml, 'w:styleId="Code"'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_style_with_color
|
||||||
|
xml_files = create_doc_and_read_all_xml do |doc|
|
||||||
|
doc.define_style :red_text, color: "FF0000"
|
||||||
|
doc.p "Red", style: :red_text
|
||||||
|
end
|
||||||
|
|
||||||
|
styles_xml = xml_files["word/styles.xml"]
|
||||||
|
assert_includes styles_xml, 'w:val="FF0000"'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_style_with_font
|
||||||
|
xml_files = create_doc_and_read_all_xml do |doc|
|
||||||
|
doc.define_style :mono, font: "Courier New"
|
||||||
|
doc.p "Mono", style: :mono
|
||||||
|
end
|
||||||
|
|
||||||
|
styles_xml = xml_files["word/styles.xml"]
|
||||||
|
assert_includes styles_xml, 'w:ascii="Courier New"'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_style_with_size
|
||||||
|
xml_files = create_doc_and_read_all_xml do |doc|
|
||||||
|
doc.define_style :big, size: 24
|
||||||
|
doc.p "Big", style: :big
|
||||||
|
end
|
||||||
|
|
||||||
|
styles_xml = xml_files["word/styles.xml"]
|
||||||
|
# 24pt = 48 half-points
|
||||||
|
assert_includes styles_xml, 'w:val="48"'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_style_with_alignment
|
||||||
|
xml_files = create_doc_and_read_all_xml do |doc|
|
||||||
|
doc.define_style :centered, align: :center
|
||||||
|
doc.p "Centered", style: :centered
|
||||||
|
end
|
||||||
|
|
||||||
|
styles_xml = xml_files["word/styles.xml"]
|
||||||
|
assert_includes styles_xml, '<w:jc w:val="center"'
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_styles_xml_generated
|
||||||
|
xml_files = create_doc_and_read_all_xml do |doc|
|
||||||
|
doc.p "Test"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert xml_files["word/styles.xml"], "styles.xml should be generated"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_styles_xml_in_content_types
|
||||||
|
xml_files = create_doc_and_read_all_xml do |doc|
|
||||||
|
doc.p "Test"
|
||||||
|
end
|
||||||
|
|
||||||
|
content_types = xml_files["[Content_Types].xml"]
|
||||||
|
assert_includes content_types, "/word/styles.xml"
|
||||||
|
assert_includes content_types, "wordprocessingml.styles+xml"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_styles_xml_in_relationships
|
||||||
|
xml_files = create_doc_and_read_all_xml do |doc|
|
||||||
|
doc.p "Test"
|
||||||
|
end
|
||||||
|
|
||||||
|
rels = xml_files["word/_rels/document.xml.rels"]
|
||||||
|
assert_includes rels, "styles.xml"
|
||||||
|
assert_includes rels, "relationships/styles"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_unknown_style_raises_error
|
||||||
|
assert_raises(ArgumentError) do
|
||||||
|
Tempfile.create(["test", ".docx"]) do |file|
|
||||||
|
Ezdoc::Document.create(file.path) do |doc|
|
||||||
|
doc.p "Test", style: :nonexistent
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_invalid_color_raises_error
|
||||||
|
assert_raises(ArgumentError) do
|
||||||
|
Ezdoc::Style.new(:bad, color: "invalid")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_invalid_alignment_raises_error
|
||||||
|
assert_raises(ArgumentError) do
|
||||||
|
Ezdoc::Style.new(:bad, align: :invalid)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_color_normalizes_hash
|
||||||
|
style = Ezdoc::Style.new(:test, color: "#ff0000")
|
||||||
|
assert_equal "FF0000", style.color
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_combined_style_properties
|
||||||
|
xml_files = create_doc_and_read_all_xml do |doc|
|
||||||
|
doc.define_style :fancy,
|
||||||
|
bold: true,
|
||||||
|
italic: true,
|
||||||
|
color: "0000FF",
|
||||||
|
size: 16,
|
||||||
|
font: "Arial",
|
||||||
|
align: :center
|
||||||
|
doc.p "Fancy", style: :fancy
|
||||||
|
end
|
||||||
|
|
||||||
|
styles_xml = xml_files["word/styles.xml"]
|
||||||
|
assert_includes styles_xml, "<w:b/>"
|
||||||
|
assert_includes styles_xml, "<w:i/>"
|
||||||
|
assert_includes styles_xml, 'w:val="0000FF"'
|
||||||
|
assert_includes styles_xml, 'w:val="32"' # 16pt = 32 half-points
|
||||||
|
assert_includes styles_xml, 'w:ascii="Arial"'
|
||||||
|
assert_includes styles_xml, '<w:jc w:val="center"'
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user