6 Commits

Author SHA1 Message Date
597bc91c40 Implement many more nodes
All checks were successful
CI Pipeline / build (pull_request) Successful in 12s
Adds these new styling and formatting nodes
* strike
* highlight
* linebreaks
* pagebreaks
* Hyperlinks
2025-12-02 14:43:53 +01:00
243b06d8f8 Not a script
All checks were successful
CI Pipeline / build (push) Successful in 11s
2025-12-02 13:46:05 +01:00
d1819f2b64 Update version
Some checks failed
CI Pipeline / build (push) Failing after 11s
2025-12-02 13:44:59 +01:00
df8fb2fb8c Update git path 2025-12-02 13:43:02 +01:00
dec346254c Project rename
All checks were successful
CI Pipeline / build (push) Successful in 12s
2025-12-02 13:21:13 +01:00
29ebb9a8d1 Merge pull request 'Fix issue where Word Online wasn't properly able to render our tables' (#5) from bug/tables-word-online into main
All checks were successful
CI Pipeline / build (push) Successful in 12s
Reviewed-on: Kaukus/ezdoc#5
2025-12-02 12:12:39 +00:00
42 changed files with 903 additions and 140 deletions

View File

@@ -0,0 +1,9 @@
{
"permissions": {
"allow": [
"Bash(bundle exec rake test:*)"
],
"deny": [],
"ask": []
}
}

View File

@@ -19,28 +19,28 @@ bundle exec ruby -Ilib:test test/paragraph_test.rb -n test_paragraph_with_text
## Architecture ## Architecture
Ezdoc is a Ruby gem for creating .docx files using a DSL. The gem generates valid Office Open XML (OOXML) documents. Notare is a Ruby gem for creating .docx files using a DSL. The gem generates valid Office Open XML (OOXML) documents.
### Core Components ### Core Components
- **Document** (`lib/ezdoc/document.rb`): Entry point via `Document.create`. Includes the Builder module and maintains a collection of nodes. - **Document** (`lib/notare/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`, `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. - **Builder** (`lib/notare/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, Image, List, ListItem, Table, TableRow, TableCell). All inherit from `Nodes::Base`. - **Nodes** (`lib/notare/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). - **Style** (`lib/notare/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/notare/package.rb`): Assembles the docx ZIP structure using rubyzip. Coordinates XML generation.
- **XML generators** (`lib/ezdoc/xml/`): Generate OOXML-compliant XML: - **XML generators** (`lib/notare/xml/`): Generate OOXML-compliant XML:
- `DocumentXml`: Main content with paragraphs, lists, tables, images - `DocumentXml`: Main content with paragraphs, lists, tables, images
- `StylesXml`: styles.xml with built-in and custom styles - `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. - **ImageDimensions** (`lib/notare/image_dimensions.rb`): Uses fastimage gem to read image dimensions for EMU calculations.
### Data Flow ### Data Flow
@@ -51,4 +51,4 @@ Ezdoc is a Ruby gem for creating .docx files using a DSL. The gem generates vali
### Testing ### Testing
Tests use Minitest. `EzdocTestHelpers` module provides helpers that create temp documents and extract XML for assertions. Tests use Minitest. `NotareTestHelpers` module provides helpers that create temp documents and extract XML for assertions.

135
README.md
View File

@@ -1,4 +1,4 @@
# Ezdoc # Notare
A Ruby gem for creating docx files with a simple DSL A Ruby gem for creating docx files with a simple DSL
@@ -7,7 +7,7 @@ A Ruby gem for creating docx files with a simple DSL
Add this line to your application's Gemfile: Add this line to your application's Gemfile:
```ruby ```ruby
gem 'ezdoc' gem 'notare'
``` ```
And then execute: And then execute:
@@ -16,16 +16,16 @@ And then execute:
Or install it yourself as: Or install it yourself as:
$ gem install ezdoc $ gem install notare
## Usage ## Usage
### Basic Example ### Basic Example
```ruby ```ruby
require 'ezdoc' require 'notare'
Ezdoc::Document.create("output.docx") do |doc| Notare::Document.create("output.docx") do |doc|
doc.p "Hello World" doc.p "Hello World"
end end
``` ```
@@ -33,7 +33,7 @@ end
### Paragraphs ### Paragraphs
```ruby ```ruby
Ezdoc::Document.create("output.docx") do |doc| Notare::Document.create("output.docx") do |doc|
# Simple paragraph # Simple paragraph
doc.p "This is a paragraph." doc.p "This is a paragraph."
@@ -50,7 +50,7 @@ end
Formatting uses nested blocks. Nesting combines formatting styles. Formatting uses nested blocks. Nesting combines formatting styles.
```ruby ```ruby
Ezdoc::Document.create("output.docx") do |doc| Notare::Document.create("output.docx") do |doc|
doc.p do doc.p do
doc.text "Normal text " doc.text "Normal text "
doc.b { doc.text "bold" } doc.b { doc.text "bold" }
@@ -58,6 +58,8 @@ Ezdoc::Document.create("output.docx") do |doc|
doc.i { doc.text "italic" } doc.i { doc.text "italic" }
doc.text " and " doc.text " and "
doc.u { doc.text "underlined" } doc.u { doc.text "underlined" }
doc.text " and "
doc.s { doc.text "strikethrough" }
end end
# Nested formatting (bold + italic) # Nested formatting (bold + italic)
@@ -66,6 +68,13 @@ Ezdoc::Document.create("output.docx") do |doc|
doc.i { doc.text "bold and italic" } doc.i { doc.text "bold and italic" }
end end
end end
# Show edits (strikethrough old, bold new)
doc.p do
doc.s { doc.text "old text" }
doc.text " "
doc.b { doc.text "new text" }
end
end end
``` ```
@@ -74,7 +83,7 @@ end
Use `h1` through `h6` for document headings: Use `h1` through `h6` for document headings:
```ruby ```ruby
Ezdoc::Document.create("output.docx") do |doc| Notare::Document.create("output.docx") do |doc|
doc.h1 "Document Title" doc.h1 "Document Title"
doc.h2 "Chapter 1" doc.h2 "Chapter 1"
doc.h3 "Section 1.1" doc.h3 "Section 1.1"
@@ -92,12 +101,12 @@ end
### Styles ### Styles
Ezdoc includes built-in styles and supports custom style definitions. Notare includes built-in styles and supports custom style definitions.
#### Built-in Styles #### Built-in Styles
```ruby ```ruby
Ezdoc::Document.create("output.docx") do |doc| Notare::Document.create("output.docx") do |doc|
doc.p "This is a title", style: :title doc.p "This is a title", style: :title
doc.p "A subtitle", style: :subtitle doc.p "A subtitle", style: :subtitle
doc.p "A quotation", style: :quote doc.p "A quotation", style: :quote
@@ -110,7 +119,7 @@ end
Define your own styles with text and paragraph properties: Define your own styles with text and paragraph properties:
```ruby ```ruby
Ezdoc::Document.create("output.docx") do |doc| Notare::Document.create("output.docx") do |doc|
# Define custom styles # Define custom styles
doc.define_style :warning, doc.define_style :warning,
bold: true, bold: true,
@@ -146,10 +155,14 @@ end
- `bold: true/false` - `bold: true/false`
- `italic: true/false` - `italic: true/false`
- `underline: true/false` - `underline: true/false`
- `strike: true/false` - strikethrough
- `highlight: "yellow"` - text highlight (see colors below)
- `color: "FF0000"` (hex RGB) - `color: "FF0000"` (hex RGB)
- `size: 14` (points) - `size: 14` (points)
- `font: "Arial"` (font family) - `font: "Arial"` (font family)
**Highlight colors:** `black`, `blue`, `cyan`, `darkBlue`, `darkCyan`, `darkGray`, `darkGreen`, `darkMagenta`, `darkRed`, `darkYellow`, `green`, `lightGray`, `magenta`, `red`, `white`, `yellow`
**Paragraph properties:** **Paragraph properties:**
- `align: :left / :center / :right / :justify` - `align: :left / :center / :right / :justify`
- `indent: 720` (twips, 1 inch = 1440 twips) - `indent: 720` (twips, 1 inch = 1440 twips)
@@ -161,7 +174,7 @@ end
#### Bullet Lists #### Bullet Lists
```ruby ```ruby
Ezdoc::Document.create("output.docx") do |doc| Notare::Document.create("output.docx") do |doc|
doc.ul do doc.ul do
doc.li "First item" doc.li "First item"
doc.li "Second item" doc.li "Second item"
@@ -173,7 +186,7 @@ end
#### Numbered Lists #### Numbered Lists
```ruby ```ruby
Ezdoc::Document.create("output.docx") do |doc| Notare::Document.create("output.docx") do |doc|
doc.ol do doc.ol do
doc.li "First" doc.li "First"
doc.li "Second" doc.li "Second"
@@ -185,7 +198,7 @@ end
### Tables ### Tables
```ruby ```ruby
Ezdoc::Document.create("output.docx") do |doc| Notare::Document.create("output.docx") do |doc|
doc.table do doc.table do
doc.tr do doc.tr do
doc.td "Header 1" doc.td "Header 1"
@@ -204,7 +217,7 @@ end
Images can be added to paragraphs, table cells, and list items. Supports PNG and JPEG formats. Images can be added to paragraphs, table cells, and list items. Supports PNG and JPEG formats.
```ruby ```ruby
Ezdoc::Document.create("output.docx") do |doc| Notare::Document.create("output.docx") do |doc|
# Simple image (uses native dimensions) # Simple image (uses native dimensions)
doc.p do doc.p do
doc.image "photo.png" doc.image "photo.png"
@@ -246,10 +259,95 @@ Ezdoc::Document.create("output.docx") do |doc|
end end
``` ```
### Line Breaks
Use `br` for soft line breaks within a paragraph (text continues in the same paragraph but on a new line):
```ruby
Notare::Document.create("output.docx") do |doc|
doc.p do
doc.text "Line one"
doc.br
doc.text "Line two (same paragraph)"
doc.br
doc.text "Line three"
end
# Useful for addresses
doc.p do
doc.b { doc.text "Address:" }
doc.br
doc.text "123 Main Street"
doc.br
doc.text "Anytown, ST 12345"
end
end
```
### Page Breaks
Use `page_break` to force content to start on a new page:
```ruby
Notare::Document.create("output.docx") do |doc|
doc.h1 "Chapter 1"
doc.p "Content of chapter 1..."
doc.page_break
doc.h1 "Chapter 2"
doc.p "This starts on a new page."
end
```
### Hyperlinks
Add clickable links with `link`:
```ruby
Notare::Document.create("output.docx") do |doc|
# Link with custom text
doc.p do
doc.text "Visit "
doc.link "https://example.com", "our website"
doc.text " for more info."
end
# Link showing the URL as text
doc.p do
doc.text "URL: "
doc.link "https://example.com"
end
# Link with formatted content
doc.p do
doc.link "https://github.com" do
doc.b { doc.text "GitHub" }
end
end
# Links in lists
doc.ul do
doc.li do
doc.link "https://ruby-lang.org", "Ruby"
end
doc.li do
doc.link "https://rubyonrails.org", "Rails"
end
end
# Email links
doc.p do
doc.text "Contact: "
doc.link "mailto:hello@example.com", "hello@example.com"
end
end
```
### Complete Example ### Complete Example
```ruby ```ruby
Ezdoc::Document.create("report.docx") do |doc| Notare::Document.create("report.docx") do |doc|
doc.p "Monthly Report" doc.p "Monthly Report"
doc.p do doc.p do
@@ -296,6 +394,11 @@ end
| `b { }` | Bold formatting | | `b { }` | Bold formatting |
| `i { }` | Italic formatting | | `i { }` | Italic formatting |
| `u { }` | Underline formatting | | `u { }` | Underline formatting |
| `s { }` | Strikethrough formatting |
| `br` | Line break (soft break within paragraph) |
| `page_break` | Page break (force new page) |
| `link(url, text)` | Hyperlink with custom text |
| `link(url) { }` | Hyperlink with block content |
| `define_style(name, **props)` | Define a custom style | | `define_style(name, **props)` | Define a custom style |
| `ul { }` | Bullet list | | `ul { }` | Bullet list |
| `ol { }` | Numbered list | | `ol { }` | Numbered list |

157
examples/full_demo.rb Executable file → Normal file
View File

@@ -1,26 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
# Full demo of all Ezdoc features # Full demo of all Notare features
# Run with: bundle exec ruby examples/full_demo.rb # Run with: bundle exec ruby examples/full_demo.rb
require_relative "../lib/ezdoc" require_relative "../lib/notare"
OUTPUT_FILE = File.expand_path("../example.docx", __dir__) OUTPUT_FILE = File.expand_path("../example.docx", __dir__)
FIXTURES_DIR = File.expand_path("../test/fixtures", __dir__) FIXTURES_DIR = File.expand_path("../test/fixtures", __dir__)
Ezdoc::Document.create(OUTPUT_FILE) do |doc| Notare::Document.create(OUTPUT_FILE) do |doc|
# ============================================================================ # ============================================================================
# Custom Styles # Custom Styles
# ============================================================================ # ============================================================================
doc.define_style :highlight, bold: true, color: "FF6600" doc.define_style :highlight, bold: true, color: "FF6600"
doc.define_style :success, color: "228B22", italic: true doc.define_style :success, color: "228B22", italic: true
doc.define_style :centered_large, align: :center, size: 16, bold: true doc.define_style :centered_large, align: :center, size: 16, bold: true
doc.define_style :deleted_text, strike: true, color: "999999"
doc.define_style :important, highlight: "yellow", bold: true
# ============================================================================ # ============================================================================
# Title and Introduction # Title and Introduction
# ============================================================================ # ============================================================================
doc.h1 "Ezdoc Feature Demo" doc.h1 "Notare Feature Demo"
doc.p "A comprehensive example of all supported features", style: :subtitle doc.p "A comprehensive example of all supported features", style: :subtitle
# ============================================================================ # ============================================================================
@@ -34,6 +35,8 @@ Ezdoc::Document.create(OUTPUT_FILE) do |doc|
doc.i { doc.text "italic" } doc.i { doc.text "italic" }
doc.text ", " doc.text ", "
doc.u { doc.text "underlined" } doc.u { doc.text "underlined" }
doc.text ", "
doc.s { doc.text "strikethrough" }
doc.text ", and " doc.text ", and "
doc.b do doc.b do
doc.i do doc.i do
@@ -43,6 +46,13 @@ Ezdoc::Document.create(OUTPUT_FILE) do |doc|
doc.text " formatting." doc.text " formatting."
end end
doc.p do
doc.text "Showing edits: "
doc.s { doc.text "old text" }
doc.text " "
doc.b { doc.text "new text" }
end
# ============================================================================ # ============================================================================
# 2. Headings # 2. Headings
# ============================================================================ # ============================================================================
@@ -74,11 +84,54 @@ Ezdoc::Document.create(OUTPUT_FILE) do |doc|
doc.text " in one paragraph." doc.text " in one paragraph."
end end
doc.p "Centered and large text", style: :centered_large doc.p "Centered and large text", style: :centered_large
doc.p "This was removed from the document", style: :deleted_text
doc.p "This is critically important!", style: :important
# ============================================================================ # ============================================================================
# 5. Lists # 5. Text Highlighting
# ============================================================================ # ============================================================================
doc.h2 "5. Lists" doc.h2 "5. Text Highlighting"
doc.p do
doc.text "You can highlight text in "
doc.text "yellow", style: :important
doc.text " or use styles with various highlight colors."
end
doc.define_style :highlight_cyan, highlight: "cyan"
doc.define_style :highlight_green, highlight: "green"
doc.define_style :highlight_magenta, highlight: "magenta"
doc.p do
doc.text "Multiple colors: "
doc.text "cyan", style: :highlight_cyan
doc.text " "
doc.text "green", style: :highlight_green
doc.text " "
doc.text "magenta", style: :highlight_magenta
end
# ============================================================================
# 6. Line Breaks
# ============================================================================
doc.h2 "6. Line Breaks"
doc.p do
doc.text "This is the first line."
doc.br
doc.text "This is the second line (soft break)."
doc.br
doc.text "This is the third line."
end
doc.p do
doc.b { doc.text "Address:" }
doc.br
doc.text "123 Main Street"
doc.br
doc.text "Anytown, ST 12345"
end
# ============================================================================
# 7. Lists
# ============================================================================
doc.h2 "7. Lists"
doc.h3 "Bullet List" doc.h3 "Bullet List"
doc.ul do doc.ul do
@@ -99,9 +152,41 @@ Ezdoc::Document.create(OUTPUT_FILE) do |doc|
end end
# ============================================================================ # ============================================================================
# 6. Tables # 8. Hyperlinks
# ============================================================================ # ============================================================================
doc.h2 "6. Tables" doc.h2 "8. Hyperlinks"
doc.p do
doc.text "Visit "
doc.link "https://www.example.com", "Example.com"
doc.text " for more information."
end
doc.p do
doc.text "Check out "
doc.link "https://github.com" do
doc.b { doc.text "GitHub" }
end
doc.text " for code hosting."
end
doc.p do
doc.text "Or just paste the URL: "
doc.link "https://www.ruby-lang.org"
end
doc.ul do
doc.li do
doc.link "https://rubyonrails.org", "Ruby on Rails"
end
doc.li do
doc.link "https://rubygems.org", "RubyGems"
end
end
# ============================================================================
# 9. Tables
# ============================================================================
doc.h2 "9. Tables"
doc.table do doc.table do
doc.tr do doc.tr do
doc.td { doc.b { doc.text "Feature" } } doc.td { doc.b { doc.text "Feature" } }
@@ -116,7 +201,27 @@ Ezdoc::Document.create(OUTPUT_FILE) do |doc|
doc.tr do doc.tr do
doc.td "Formatting" doc.td "Formatting"
doc.td { doc.text "Complete", style: :success } doc.td { doc.text "Complete", style: :success }
doc.td "Bold, italic, underline" doc.td "Bold, italic, underline, strikethrough"
end
doc.tr do
doc.td "Highlighting"
doc.td { doc.text "Complete", style: :success }
doc.td "16 highlight colors"
end
doc.tr do
doc.td "Line Breaks"
doc.td { doc.text "Complete", style: :success }
doc.td "Soft breaks within paragraphs"
end
doc.tr do
doc.td "Page Breaks"
doc.td { doc.text "Complete", style: :success }
doc.td "Force new pages"
end
doc.tr do
doc.td "Hyperlinks"
doc.td { doc.text "Complete", style: :success }
doc.td "Clickable links"
end end
doc.tr do doc.tr do
doc.td "Headings" doc.td "Headings"
@@ -136,9 +241,9 @@ Ezdoc::Document.create(OUTPUT_FILE) do |doc|
end end
# ============================================================================ # ============================================================================
# 7. Images # 10. Images
# ============================================================================ # ============================================================================
doc.h2 "7. Images" doc.h2 "10. Images"
doc.p "Image with explicit dimensions:" doc.p "Image with explicit dimensions:"
doc.p do doc.p do
@@ -163,9 +268,19 @@ Ezdoc::Document.create(OUTPUT_FILE) do |doc|
end end
# ============================================================================ # ============================================================================
# 8. Combined Features # 11. Page Breaks
# ============================================================================ # ============================================================================
doc.h2 "8. Combined Features" doc.h2 "11. Page Breaks"
doc.p "The next element will force a new page."
doc.page_break
# ============================================================================
# 12. Combined Features (on new page)
# ============================================================================
doc.h2 "12. Combined Features"
doc.p "This section starts on a new page thanks to the page break above."
doc.p do doc.p do
doc.text "This final paragraph combines " doc.text "This final paragraph combines "
doc.b { doc.text "multiple" } doc.b { doc.text "multiple" }
@@ -173,7 +288,19 @@ Ezdoc::Document.create(OUTPUT_FILE) do |doc|
doc.i { doc.text "formatting" } doc.i { doc.text "formatting" }
doc.text " options with " doc.text " options with "
doc.text "custom styles", style: :highlight doc.text "custom styles", style: :highlight
doc.text " to demonstrate the full power of Ezdoc." doc.text ", "
doc.s { doc.text "strikethrough" }
doc.text ", "
doc.text "highlighting", style: :important
doc.text ", and "
doc.link "https://example.com", "hyperlinks"
doc.text " to demonstrate the full power of Notare."
end
doc.p do
doc.text "Contact us:"
doc.br
doc.link "mailto:hello@example.com", "hello@example.com"
end end
doc.p "End of demo document.", style: :centered_large doc.p "End of demo document.", style: :centered_large

View File

@@ -1,28 +0,0 @@
# frozen_string_literal: true
require "nokogiri"
require_relative "ezdoc/version"
require_relative "ezdoc/nodes/base"
require_relative "ezdoc/nodes/run"
require_relative "ezdoc/nodes/image"
require_relative "ezdoc/nodes/paragraph"
require_relative "ezdoc/nodes/list"
require_relative "ezdoc/nodes/list_item"
require_relative "ezdoc/nodes/table"
require_relative "ezdoc/nodes/table_row"
require_relative "ezdoc/nodes/table_cell"
require_relative "ezdoc/image_dimensions"
require_relative "ezdoc/style"
require_relative "ezdoc/xml/content_types"
require_relative "ezdoc/xml/relationships"
require_relative "ezdoc/xml/document_xml"
require_relative "ezdoc/xml/numbering"
require_relative "ezdoc/xml/styles_xml"
require_relative "ezdoc/builder"
require_relative "ezdoc/package"
require_relative "ezdoc/document"
module Ezdoc
class Error < StandardError; end
end

30
lib/notare.rb Normal file
View File

@@ -0,0 +1,30 @@
# frozen_string_literal: true
require "nokogiri"
require_relative "notare/version"
require_relative "notare/nodes/base"
require_relative "notare/nodes/break"
require_relative "notare/nodes/hyperlink"
require_relative "notare/nodes/run"
require_relative "notare/nodes/image"
require_relative "notare/nodes/paragraph"
require_relative "notare/nodes/list"
require_relative "notare/nodes/list_item"
require_relative "notare/nodes/table"
require_relative "notare/nodes/table_row"
require_relative "notare/nodes/table_cell"
require_relative "notare/image_dimensions"
require_relative "notare/style"
require_relative "notare/xml/content_types"
require_relative "notare/xml/relationships"
require_relative "notare/xml/document_xml"
require_relative "notare/xml/numbering"
require_relative "notare/xml/styles_xml"
require_relative "notare/builder"
require_relative "notare/package"
require_relative "notare/document"
module Notare
class Error < StandardError; end
end

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Builder module Builder
def p(text = nil, style: nil, &block) def p(text = nil, style: nil, &block)
para = Nodes::Paragraph.new(style: resolve_style(style)) para = Nodes::Paragraph.new(style: resolve_style(style))
@@ -60,6 +60,30 @@ module Ezdoc
with_format(:underline, &block) with_format(:underline, &block)
end end
def s(&block)
with_format(:strike, &block)
end
def br
@current_target.add_run(Nodes::Break.new(type: :line))
end
def page_break
@nodes << Nodes::Break.new(type: :page)
end
def link(url, text = nil, &block)
hyperlink = register_hyperlink(url)
if block
with_target(hyperlink, &block)
elsif text
hyperlink.add_run(Nodes::Run.new(text, underline: true, color: "0000FF"))
else
hyperlink.add_run(Nodes::Run.new(url, underline: true, color: "0000FF"))
end
@current_target.add_run(hyperlink)
end
def ul(&block) def ul(&block)
list(:bullet, &block) list(:bullet, &block)
end end
@@ -111,6 +135,7 @@ module Ezdoc
def list(type, &block) def list(type, &block)
@num_id_counter ||= 0 @num_id_counter ||= 0
@num_id_counter += 1 @num_id_counter += 1
mark_has_lists!
list_node = Nodes::List.new(type: type, num_id: @num_id_counter) list_node = Nodes::List.new(type: type, num_id: @num_id_counter)
previous_list = @current_list previous_list = @current_list
@@ -139,7 +164,8 @@ module Ezdoc
{ {
bold: @format_stack.include?(:bold), bold: @format_stack.include?(:bold),
italic: @format_stack.include?(:italic), italic: @format_stack.include?(:italic),
underline: @format_stack.include?(:underline) underline: @format_stack.include?(:underline),
strike: @format_stack.include?(:strike)
} }
end end

View File

@@ -1,10 +1,10 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
class Document class Document
include Builder include Builder
attr_reader :nodes, :styles attr_reader :nodes, :styles, :hyperlinks
def self.create(path, &block) def self.create(path, &block)
doc = new doc = new
@@ -21,7 +21,9 @@ module Ezdoc
@current_table = nil @current_table = nil
@current_row = nil @current_row = nil
@num_id_counter = 0 @num_id_counter = 0
@has_lists = false
@images = {} @images = {}
@hyperlinks = []
@styles = {} @styles = {}
register_built_in_styles register_built_in_styles
end end
@@ -42,10 +44,25 @@ module Ezdoc
@nodes.select { |n| n.is_a?(Nodes::List) } @nodes.select { |n| n.is_a?(Nodes::List) }
end end
def uses_lists?
@has_lists
end
def mark_has_lists!
@has_lists = true
end
def images def images
@images.values @images.values
end end
def register_hyperlink(url)
rid = next_hyperlink_rid
hyperlink = Nodes::Hyperlink.new(url: url, rid: rid)
@hyperlinks << hyperlink
hyperlink
end
def register_image(path, width: nil, height: nil) def register_image(path, width: nil, height: nil)
return @images[path] if @images[path] return @images[path] if @images[path]
@@ -61,11 +78,17 @@ module Ezdoc
def next_image_rid def next_image_rid
# rId1 = styles.xml (always present) # rId1 = styles.xml (always present)
# rId2 = numbering.xml (if lists present) # rId2 = numbering.xml (if lists present)
# rId3+ = images # rId3+ = images, then hyperlinks
base = lists.any? ? 3 : 2 base = @has_lists ? 3 : 2
"rId#{base + @images.size}" "rId#{base + @images.size}"
end end
def next_hyperlink_rid
# Hyperlinks come after images
base = @has_lists ? 3 : 2
"rId#{base + @images.size + @hyperlinks.size}"
end
def register_built_in_styles def register_built_in_styles
# Headings (spacing_before ensures they're rendered as paragraph styles) # Headings (spacing_before ensures they're rendered as paragraph styles)
define_style :heading1, size: 24, bold: true, spacing_before: 240, spacing_after: 120 define_style :heading1, size: 24, bold: true, spacing_before: 240, spacing_after: 120

View File

@@ -2,7 +2,7 @@
require "fastimage" require "fastimage"
module Ezdoc module Notare
class ImageDimensions class ImageDimensions
EMUS_PER_INCH = 914_400 EMUS_PER_INCH = 914_400
DEFAULT_DPI = 96 DEFAULT_DPI = 96

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Nodes module Nodes
class Base class Base
# Base class for all document nodes # Base class for all document nodes

18
lib/notare/nodes/break.rb Normal file
View File

@@ -0,0 +1,18 @@
# frozen_string_literal: true
module Notare
module Nodes
class Break < Base
attr_reader :type
def initialize(type: :line)
super()
@type = type
end
def page?
type == :page
end
end
end
end

View File

@@ -0,0 +1,20 @@
# frozen_string_literal: true
module Notare
module Nodes
class Hyperlink < Base
attr_reader :url, :rid, :runs
def initialize(url:, rid:)
super()
@url = url
@rid = rid
@runs = []
end
def add_run(run)
@runs << run
end
end
end
end

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Nodes module Nodes
class Image < Base class Image < Base
attr_reader :path, :width_emu, :height_emu, :rid, :filename attr_reader :path, :width_emu, :height_emu, :rid, :filename

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Nodes module Nodes
class List < Base class List < Base
attr_reader :items, :type, :num_id attr_reader :items, :type, :num_id

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Nodes module Nodes
class ListItem < Base class ListItem < Base
attr_reader :runs, :list_type, :num_id attr_reader :runs, :list_type, :num_id

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Nodes module Nodes
class Paragraph < Base class Paragraph < Base
attr_reader :runs, :style attr_reader :runs, :style

View File

@@ -1,16 +1,20 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Nodes module Nodes
class Run < Base class Run < Base
attr_reader :text, :bold, :italic, :underline, :style attr_reader :text, :bold, :italic, :underline, :strike, :highlight, :color, :style
def initialize(text, bold: false, italic: false, underline: false, style: nil) def initialize(text, bold: false, italic: false, underline: false,
strike: false, highlight: nil, color: nil, style: nil)
super() super()
@text = text @text = text
@bold = bold @bold = bold
@italic = italic @italic = italic
@underline = underline @underline = underline
@strike = strike
@highlight = highlight
@color = color
@style = style @style = style
end end
end end

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Nodes module Nodes
class Table < Base class Table < Base
attr_reader :rows attr_reader :rows

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Nodes module Nodes
class TableCell < Base class TableCell < Base
attr_reader :runs attr_reader :runs

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Nodes module Nodes
class TableRow < Base class TableRow < Base
attr_reader :cells attr_reader :cells

View File

@@ -2,7 +2,7 @@
require "zip" require "zip"
module Ezdoc module Notare
class Package class Package
def initialize(document) def initialize(document)
@document = document @document = document
@@ -29,13 +29,17 @@ module Ezdoc
private private
def lists? def lists?
@document.lists.any? @document.uses_lists?
end end
def images def images
@document.images @document.images
end end
def hyperlinks
@document.hyperlinks
end
def content_types_xml def content_types_xml
Xml::ContentTypes.new(has_numbering: lists?, images: images, has_styles: true).to_xml Xml::ContentTypes.new(has_numbering: lists?, images: images, has_styles: true).to_xml
end end
@@ -45,7 +49,9 @@ module Ezdoc
end end
def document_relationships_xml def document_relationships_xml
Xml::DocumentRelationships.new(has_numbering: lists?, images: images, has_styles: true).to_xml Xml::DocumentRelationships.new(
has_numbering: lists?, images: images, hyperlinks: hyperlinks, has_styles: true
).to_xml
end end
def document_xml def document_xml

View File

@@ -1,19 +1,25 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
class Style class Style
attr_reader :name, :bold, :italic, :underline, :color, :size, :font, attr_reader :name, :bold, :italic, :underline, :strike, :highlight, :color, :size, :font,
:align, :indent, :spacing_before, :spacing_after :align, :indent, :spacing_before, :spacing_after
ALIGNMENTS = %i[left center right justify].freeze ALIGNMENTS = %i[left center right justify].freeze
HIGHLIGHT_COLORS = %w[
black blue cyan darkBlue darkCyan darkGray darkGreen darkMagenta
darkRed darkYellow green lightGray magenta red white yellow
].freeze
def initialize(name, bold: nil, italic: nil, underline: nil, color: nil, def initialize(name, bold: nil, italic: nil, underline: nil, strike: nil,
size: nil, font: nil, align: nil, indent: nil, highlight: nil, color: nil, size: nil, font: nil, align: nil,
spacing_before: nil, spacing_after: nil) indent: nil, spacing_before: nil, spacing_after: nil)
@name = name @name = name
@bold = bold @bold = bold
@italic = italic @italic = italic
@underline = underline @underline = underline
@strike = strike
@highlight = validate_highlight(highlight)
@color = normalize_color(color) @color = normalize_color(color)
@size = size @size = size
@font = font @font = font
@@ -36,7 +42,7 @@ module Ezdoc
end end
def text_properties? def text_properties?
!!(bold || italic || underline || color || size || font) !!(bold || italic || underline || strike || highlight || color || size || font)
end end
# Size in half-points for OOXML (14pt = 28 half-points) # Size in half-points for OOXML (14pt = 28 half-points)
@@ -61,5 +67,14 @@ module Ezdoc
raise ArgumentError, "Invalid alignment: #{align}. Use #{ALIGNMENTS.join(", ")}" raise ArgumentError, "Invalid alignment: #{align}. Use #{ALIGNMENTS.join(", ")}"
end end
def validate_highlight(highlight)
return nil if highlight.nil?
color = highlight.to_s
return color if HIGHLIGHT_COLORS.include?(color)
raise ArgumentError, "Invalid highlight color: #{highlight}. Use one of: #{HIGHLIGHT_COLORS.join(", ")}"
end
end end
end end

View File

@@ -1,5 +1,5 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
VERSION = "0.0.1" VERSION = "0.0.2"
end end

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Xml module Xml
class ContentTypes class ContentTypes
NAMESPACE = "http://schemas.openxmlformats.org/package/2006/content-types" NAMESPACE = "http://schemas.openxmlformats.org/package/2006/content-types"

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Xml module Xml
class DocumentXml class DocumentXml
NAMESPACES = { NAMESPACES = {
@@ -37,6 +37,16 @@ module Ezdoc
render_list(xml, node) render_list(xml, node)
when Nodes::Table when Nodes::Table
render_table(xml, node) render_table(xml, node)
when Nodes::Break
render_page_break(xml, node)
end
end
def render_page_break(xml, _node)
xml["w"].p do
xml["w"].r do
xml["w"].br("w:type" => "page")
end
end end
end end
@@ -71,19 +81,42 @@ module Ezdoc
case run case run
when Nodes::Image when Nodes::Image
render_image(xml, run) render_image(xml, run)
when Nodes::Break
render_break(xml, run)
when Nodes::Hyperlink
render_hyperlink(xml, run)
when Nodes::Run when Nodes::Run
render_text_run(xml, run) render_text_run(xml, run)
end end
end end
def render_hyperlink(xml, hyperlink)
xml["w"].hyperlink("r:id" => hyperlink.rid) do
hyperlink.runs.each { |run| render_run(xml, run) }
end
end
def render_break(xml, break_node)
xml["w"].r do
if break_node.page?
xml["w"].br("w:type" => "page")
else
xml["w"].br
end
end
end
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 || run.style if run.bold || run.italic || run.underline || run.strike || run.highlight || run.color || run.style
xml["w"].rPr do xml["w"].rPr do
xml["w"].rStyle("w:val" => run.style.style_id) if run.style 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
xml["w"].strike if run.strike
xml["w"].highlight("w:val" => run.highlight) if run.highlight
xml["w"].color("w:val" => run.color) if run.color
end end
end end
xml["w"].t(run.text, "xml:space" => "preserve") xml["w"].t(run.text, "xml:space" => "preserve")

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Xml module Xml
class Numbering class Numbering
NAMESPACE = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" NAMESPACE = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Xml module Xml
class Relationships class Relationships
NAMESPACE = "http://schemas.openxmlformats.org/package/2006/relationships" NAMESPACE = "http://schemas.openxmlformats.org/package/2006/relationships"
@@ -24,10 +24,12 @@ module Ezdoc
STYLES_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" STYLES_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
NUMBERING_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" 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"
HYPERLINK_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
def initialize(has_numbering: false, images: [], has_styles: false) def initialize(has_numbering: false, images: [], hyperlinks: [], has_styles: false)
@has_numbering = has_numbering @has_numbering = has_numbering
@images = images @images = images
@hyperlinks = hyperlinks
@has_styles = has_styles @has_styles = has_styles
end end
@@ -60,6 +62,16 @@ module Ezdoc
Target: "media/#{image.filename}" Target: "media/#{image.filename}"
) )
end end
# Hyperlinks come after images
@hyperlinks.each do |hyperlink|
xml.Relationship(
Id: hyperlink.rid,
Type: HYPERLINK_TYPE,
Target: hyperlink.url,
TargetMode: "External"
)
end
end end
end end
builder.to_xml builder.to_xml

View File

@@ -1,6 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
module Ezdoc module Notare
module Xml module Xml
class StylesXml class StylesXml
NAMESPACE = "http://schemas.openxmlformats.org/wordprocessingml/2006/main" NAMESPACE = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
@@ -59,6 +59,8 @@ module Ezdoc
xml["w"].b if style.bold xml["w"].b if style.bold
xml["w"].i if style.italic xml["w"].i if style.italic
xml["w"].u("w:val" => "single") if style.underline xml["w"].u("w:val" => "single") if style.underline
xml["w"].strike if style.strike
xml["w"].highlight("w:val" => style.highlight) if style.highlight
end end
end end
end end

View File

@@ -1,14 +1,14 @@
# frozen_string_literal: true # frozen_string_literal: true
require_relative "lib/ezdoc/version" require_relative "lib/notare/version"
Gem::Specification.new do |spec| Gem::Specification.new do |spec|
spec.name = "ezdoc" spec.name = "notare"
spec.version = Ezdoc::VERSION spec.version = Notare::VERSION
spec.authors = ["Mathias"] spec.authors = ["Mathias"]
spec.summary = "A Ruby gem for working with docx files" spec.summary = "A Ruby gem for creating docx files with a simple DSL"
spec.description = "Easy document manipulation for docx files in Ruby" spec.description = "Notare provides a clean DSL for creating Word documents in Ruby"
spec.homepage = "https://github.com/mathias/ezdoc" spec.homepage = "https://git.kaukus.no/Kaukus/Notare"
spec.license = "MIT" spec.license = "MIT"
spec.required_ruby_version = ">= 3.0.0" spec.required_ruby_version = ">= 3.0.0"

View File

@@ -3,11 +3,11 @@
require "test_helper" require "test_helper"
class DocumentTest < Minitest::Test class DocumentTest < Minitest::Test
include EzdocTestHelpers include NotareTestHelpers
def test_creates_valid_docx_structure def test_creates_valid_docx_structure
Tempfile.create(["test", ".docx"]) do |file| Tempfile.create(["test", ".docx"]) do |file|
Ezdoc::Document.create(file.path) do |doc| Notare::Document.create(file.path) do |doc|
doc.p "Test" doc.p "Test"
end end
@@ -33,7 +33,7 @@ class DocumentTest < Minitest::Test
def test_empty_document def test_empty_document
Tempfile.create(["test", ".docx"]) do |file| Tempfile.create(["test", ".docx"]) do |file|
Ezdoc::Document.create(file.path) { |_doc| } # rubocop:disable Lint/EmptyBlock Notare::Document.create(file.path) { |_doc| } # rubocop:disable Lint/EmptyBlock
assert File.exist?(file.path) assert File.exist?(file.path)
Zip::File.open(file.path) do |zip| Zip::File.open(file.path) do |zip|
@@ -45,30 +45,30 @@ class DocumentTest < Minitest::Test
def test_document_create_returns_document def test_document_create_returns_document
result = nil result = nil
Tempfile.create(["test", ".docx"]) do |file| Tempfile.create(["test", ".docx"]) do |file|
result = Ezdoc::Document.create(file.path) do |doc| result = Notare::Document.create(file.path) do |doc|
doc.p "Test" doc.p "Test"
end end
end end
assert_instance_of Ezdoc::Document, result assert_instance_of Notare::Document, result
end end
def test_document_has_nodes def test_document_has_nodes
doc = Ezdoc::Document.new doc = Notare::Document.new
doc.p "Test" doc.p "Test"
assert_equal 1, doc.nodes.count assert_equal 1, doc.nodes.count
assert_instance_of Ezdoc::Nodes::Paragraph, doc.nodes.first assert_instance_of Notare::Nodes::Paragraph, doc.nodes.first
end end
def test_document_lists_helper def test_document_lists_helper
doc = Ezdoc::Document.new doc = Notare::Document.new
doc.p "Paragraph" doc.p "Paragraph"
doc.ul { doc.li "Bullet" } doc.ul { doc.li "Bullet" }
doc.ol { doc.li "Number" } doc.ol { doc.li "Number" }
doc.table { doc.tr { doc.td "Cell" } } doc.table { doc.tr { doc.td "Cell" } }
assert_equal 2, doc.lists.count assert_equal 2, doc.lists.count
assert(doc.lists.all? { |l| l.is_a?(Ezdoc::Nodes::List) }) assert(doc.lists.all? { |l| l.is_a?(Notare::Nodes::List) })
end end
end end

View File

@@ -3,7 +3,7 @@
require "test_helper" require "test_helper"
class FormattingTest < Minitest::Test class FormattingTest < Minitest::Test
include EzdocTestHelpers include NotareTestHelpers
def test_bold_text def test_bold_text
xml = create_doc_and_read_xml do |doc| xml = create_doc_and_read_xml do |doc|
@@ -133,4 +133,49 @@ class FormattingTest < Minitest::Test
assert_includes xml, "bold+italic " assert_includes xml, "bold+italic "
assert_includes xml, "all three" assert_includes xml, "all three"
end end
def test_strikethrough_text
xml = create_doc_and_read_xml do |doc|
doc.p do
doc.s { doc.text "strikethrough text" }
end
end
assert_includes xml, "<w:strike/>"
assert_includes xml, "strikethrough text"
end
def test_strikethrough_with_other_formatting
xml = create_doc_and_read_xml do |doc|
doc.p do
doc.b do
doc.s { doc.text "bold and strikethrough" }
end
end
end
assert_includes xml, "<w:b/>"
assert_includes xml, "<w:strike/>"
assert_includes xml, "bold and strikethrough"
end
def test_all_four_formatting_options
xml = create_doc_and_read_xml do |doc|
doc.p do
doc.b do
doc.i do
doc.u do
doc.s { doc.text "all four" }
end
end
end
end
end
assert_includes xml, "<w:b/>"
assert_includes xml, "<w:i/>"
assert_includes xml, '<w:u w:val="single"/>'
assert_includes xml, "<w:strike/>"
assert_includes xml, "all four"
end
end end

View File

@@ -3,7 +3,7 @@
require "test_helper" require "test_helper"
class HeadingTest < Minitest::Test class HeadingTest < Minitest::Test
include EzdocTestHelpers include NotareTestHelpers
def test_h1 def test_h1
xml = create_doc_and_read_xml { |doc| doc.h1 "Title" } xml = create_doc_and_read_xml { |doc| doc.h1 "Title" }

127
test/hyperlink_test.rb Normal file
View File

@@ -0,0 +1,127 @@
# frozen_string_literal: true
require "test_helper"
class HyperlinkTest < Minitest::Test
include NotareTestHelpers
def test_simple_hyperlink
xml = create_doc_and_read_xml do |doc|
doc.p do
doc.link "https://example.com", "Example"
end
end
assert_includes xml, '<w:hyperlink r:id="rId2"'
assert_includes xml, "Example"
assert_includes xml, '<w:u w:val="single"/>'
assert_includes xml, '<w:color w:val="0000FF"/>'
end
def test_hyperlink_url_as_text
xml = create_doc_and_read_xml do |doc|
doc.p do
doc.link "https://example.com"
end
end
assert_includes xml, "https://example.com"
end
def test_hyperlink_with_block
xml = create_doc_and_read_xml do |doc|
doc.p do
doc.link "https://example.com" do
doc.b { doc.text "Bold Link" }
end
end
end
assert_includes xml, '<w:hyperlink r:id="rId2"'
assert_includes xml, "<w:b/>"
assert_includes xml, "Bold Link"
end
def test_hyperlink_relationship
xml_files = create_doc_and_read_all_xml do |doc|
doc.p do
doc.link "https://example.com", "Example"
end
end
rels = xml_files["word/_rels/document.xml.rels"]
assert_includes rels, "https://example.com"
assert_includes rels, 'TargetMode="External"'
assert_includes rels, "relationships/hyperlink"
end
def test_multiple_hyperlinks
xml = create_doc_and_read_xml do |doc|
doc.p do
doc.link "https://one.com", "One"
doc.text " and "
doc.link "https://two.com", "Two"
end
end
assert_includes xml, '<w:hyperlink r:id="rId2"'
assert_includes xml, '<w:hyperlink r:id="rId3"'
assert_includes xml, "One"
assert_includes xml, "Two"
end
def test_hyperlink_in_list_item
xml = create_doc_and_read_xml do |doc|
doc.ul do
doc.li do
doc.link "https://example.com", "Link in list"
end
end
end
assert_includes xml, '<w:hyperlink r:id="rId3"'
assert_includes xml, "Link in list"
end
def test_hyperlink_in_table_cell
xml = create_doc_and_read_xml do |doc|
doc.table do
doc.tr do
doc.td do
doc.link "https://example.com", "Link in table"
end
end
end
end
assert_includes xml, '<w:hyperlink r:id="rId2"'
assert_includes xml, "Link in table"
end
def test_hyperlink_with_surrounding_text
xml = create_doc_and_read_xml do |doc|
doc.p do
doc.text "Visit "
doc.link "https://example.com", "our site"
doc.text " for more info."
end
end
assert_includes xml, "Visit "
assert_includes xml, "our site"
assert_includes xml, " for more info."
end
def test_multiple_hyperlinks_relationships
xml_files = create_doc_and_read_all_xml do |doc|
doc.p do
doc.link "https://one.com", "One"
doc.link "https://two.com", "Two"
end
end
rels = xml_files["word/_rels/document.xml.rels"]
assert_includes rels, "https://one.com"
assert_includes rels, "https://two.com"
end
end

View File

@@ -3,7 +3,7 @@
require "test_helper" require "test_helper"
class ImageTest < Minitest::Test class ImageTest < Minitest::Test
include EzdocTestHelpers include NotareTestHelpers
def setup def setup
@png_path = File.expand_path("fixtures/test.png", __dir__) @png_path = File.expand_path("fixtures/test.png", __dir__)
@@ -80,7 +80,7 @@ class ImageTest < Minitest::Test
def test_image_file_embedded_in_docx def test_image_file_embedded_in_docx
files = nil files = nil
Tempfile.create(["test", ".docx"]) do |file| Tempfile.create(["test", ".docx"]) do |file|
Ezdoc::Document.create(file.path) do |doc| Notare::Document.create(file.path) do |doc|
doc.p { doc.image @png_path } doc.p { doc.image @png_path }
end end
Zip::File.open(file.path) do |zip| Zip::File.open(file.path) do |zip|
@@ -124,7 +124,7 @@ class ImageTest < Minitest::Test
def test_invalid_image_path_raises_error def test_invalid_image_path_raises_error
assert_raises(ArgumentError) do assert_raises(ArgumentError) do
Tempfile.create(["test", ".docx"]) do |file| Tempfile.create(["test", ".docx"]) do |file|
Ezdoc::Document.create(file.path) do |doc| Notare::Document.create(file.path) do |doc|
doc.p { doc.image "/nonexistent/image.png" } doc.p { doc.image "/nonexistent/image.png" }
end end
end end
@@ -138,7 +138,7 @@ class ImageTest < Minitest::Test
assert_raises(ArgumentError) do assert_raises(ArgumentError) do
Tempfile.create(["test", ".docx"]) do |docx_file| Tempfile.create(["test", ".docx"]) do |docx_file|
Ezdoc::Document.create(docx_file.path) do |doc| Notare::Document.create(docx_file.path) do |doc|
doc.p { doc.image gif_file.path } doc.p { doc.image gif_file.path }
end end
end end
@@ -149,7 +149,7 @@ class ImageTest < Minitest::Test
def test_same_image_used_multiple_times_deduplication def test_same_image_used_multiple_times_deduplication
files = nil files = nil
Tempfile.create(["test", ".docx"]) do |file| Tempfile.create(["test", ".docx"]) do |file|
Ezdoc::Document.create(file.path) do |doc| Notare::Document.create(file.path) do |doc|
doc.p { doc.image @png_path } doc.p { doc.image @png_path }
doc.p { doc.image @png_path } doc.p { doc.image @png_path }
doc.p { doc.image @png_path } doc.p { doc.image @png_path }
@@ -166,7 +166,7 @@ class ImageTest < Minitest::Test
def test_multiple_different_images def test_multiple_different_images
files = nil files = nil
Tempfile.create(["test", ".docx"]) do |file| Tempfile.create(["test", ".docx"]) do |file|
Ezdoc::Document.create(file.path) do |doc| Notare::Document.create(file.path) do |doc|
doc.p { doc.image @png_path } doc.p { doc.image @png_path }
doc.p { doc.image @jpeg_path } doc.p { doc.image @jpeg_path }
end end

View File

@@ -3,7 +3,7 @@
require "test_helper" require "test_helper"
class IntegrationTest < Minitest::Test class IntegrationTest < Minitest::Test
include EzdocTestHelpers include NotareTestHelpers
def test_complex_document_with_all_features def test_complex_document_with_all_features
xml_files = create_doc_and_read_all_xml do |doc| xml_files = create_doc_and_read_all_xml do |doc|

85
test/line_break_test.rb Normal file
View File

@@ -0,0 +1,85 @@
# frozen_string_literal: true
require "test_helper"
class LineBreakTest < Minitest::Test
include NotareTestHelpers
def test_line_break_in_paragraph
xml = create_doc_and_read_xml do |doc|
doc.p do
doc.text "First line"
doc.br
doc.text "Second line"
end
end
assert_includes xml, "First line"
assert_includes xml, "Second line"
assert_includes xml, "<w:br/>"
end
def test_multiple_line_breaks
xml = create_doc_and_read_xml do |doc|
doc.p do
doc.text "Line 1"
doc.br
doc.text "Line 2"
doc.br
doc.text "Line 3"
end
end
assert_equal 2, xml.scan("<w:br/>").count
end
def test_line_break_with_formatting
xml = create_doc_and_read_xml do |doc|
doc.p do
doc.b { doc.text "Bold first line" }
doc.br
doc.i { doc.text "Italic second line" }
end
end
assert_includes xml, "<w:b/>"
assert_includes xml, "<w:i/>"
assert_includes xml, "<w:br/>"
assert_includes xml, "Bold first line"
assert_includes xml, "Italic second line"
end
def test_line_break_in_list_item
xml = create_doc_and_read_xml do |doc|
doc.ul do
doc.li do
doc.text "First line"
doc.br
doc.text "Second line"
end
end
end
assert_includes xml, "<w:br/>"
assert_includes xml, "First line"
assert_includes xml, "Second line"
end
def test_line_break_in_table_cell
xml = create_doc_and_read_xml do |doc|
doc.table do
doc.tr do
doc.td do
doc.text "Cell line 1"
doc.br
doc.text "Cell line 2"
end
end
end
end
assert_includes xml, "<w:br/>"
assert_includes xml, "Cell line 1"
assert_includes xml, "Cell line 2"
end
end

View File

@@ -3,7 +3,7 @@
require "test_helper" require "test_helper"
class ListTest < Minitest::Test class ListTest < Minitest::Test
include EzdocTestHelpers include NotareTestHelpers
# #
# Bullet List Tests # Bullet List Tests

54
test/page_break_test.rb Normal file
View File

@@ -0,0 +1,54 @@
# frozen_string_literal: true
require "test_helper"
class PageBreakTest < Minitest::Test
include NotareTestHelpers
def test_page_break
xml = create_doc_and_read_xml do |doc|
doc.p "First page content"
doc.page_break
doc.p "Second page content"
end
assert_includes xml, "First page content"
assert_includes xml, "Second page content"
assert_includes xml, '<w:br w:type="page"/>'
end
def test_multiple_page_breaks
xml = create_doc_and_read_xml do |doc|
doc.p "Page 1"
doc.page_break
doc.p "Page 2"
doc.page_break
doc.p "Page 3"
end
assert_equal 2, xml.scan('<w:br w:type="page"/>').count
end
def test_page_break_between_different_elements
xml = create_doc_and_read_xml do |doc|
doc.h1 "Chapter 1"
doc.p "Some content"
doc.page_break
doc.h1 "Chapter 2"
doc.ul do
doc.li "Item 1"
end
end
assert_includes xml, "Chapter 1"
assert_includes xml, "Chapter 2"
assert_includes xml, '<w:br w:type="page"/>'
end
def test_page_break_renders_in_own_paragraph
xml = create_doc_and_read_xml(&:page_break)
# Page break should be wrapped in its own paragraph
assert_match(%r{<w:p>\s*<w:r>\s*<w:br w:type="page"/>\s*</w:r>\s*</w:p>}m, xml)
end
end

View File

@@ -3,7 +3,7 @@
require "test_helper" require "test_helper"
class ParagraphTest < Minitest::Test class ParagraphTest < Minitest::Test
include EzdocTestHelpers include NotareTestHelpers
def test_simple_paragraph def test_simple_paragraph
xml = create_doc_and_read_xml { |doc| doc.p "Hello World" } xml = create_doc_and_read_xml { |doc| doc.p "Hello World" }

View File

@@ -3,7 +3,7 @@
require "test_helper" require "test_helper"
class StyleTest < Minitest::Test class StyleTest < Minitest::Test
include EzdocTestHelpers include NotareTestHelpers
def test_define_custom_style def test_define_custom_style
xml_files = create_doc_and_read_all_xml do |doc| xml_files = create_doc_and_read_all_xml do |doc|
@@ -124,7 +124,7 @@ class StyleTest < Minitest::Test
def test_unknown_style_raises_error def test_unknown_style_raises_error
assert_raises(ArgumentError) do assert_raises(ArgumentError) do
Tempfile.create(["test", ".docx"]) do |file| Tempfile.create(["test", ".docx"]) do |file|
Ezdoc::Document.create(file.path) do |doc| Notare::Document.create(file.path) do |doc|
doc.p "Test", style: :nonexistent doc.p "Test", style: :nonexistent
end end
end end
@@ -133,18 +133,18 @@ class StyleTest < Minitest::Test
def test_invalid_color_raises_error def test_invalid_color_raises_error
assert_raises(ArgumentError) do assert_raises(ArgumentError) do
Ezdoc::Style.new(:bad, color: "invalid") Notare::Style.new(:bad, color: "invalid")
end end
end end
def test_invalid_alignment_raises_error def test_invalid_alignment_raises_error
assert_raises(ArgumentError) do assert_raises(ArgumentError) do
Ezdoc::Style.new(:bad, align: :invalid) Notare::Style.new(:bad, align: :invalid)
end end
end end
def test_color_normalizes_hash def test_color_normalizes_hash
style = Ezdoc::Style.new(:test, color: "#ff0000") style = Notare::Style.new(:test, color: "#ff0000")
assert_equal "FF0000", style.color assert_equal "FF0000", style.color
end end
@@ -168,4 +168,56 @@ class StyleTest < Minitest::Test
assert_includes styles_xml, 'w:ascii="Arial"' assert_includes styles_xml, 'w:ascii="Arial"'
assert_includes styles_xml, '<w:jc w:val="center"' assert_includes styles_xml, '<w:jc w:val="center"'
end end
def test_style_with_highlight
xml_files = create_doc_and_read_all_xml do |doc|
doc.define_style :highlighted, highlight: "yellow"
doc.p "Highlighted", style: :highlighted
end
styles_xml = xml_files["word/styles.xml"]
assert_includes styles_xml, '<w:highlight w:val="yellow"'
end
def test_style_with_strikethrough
xml_files = create_doc_and_read_all_xml do |doc|
doc.define_style :deleted, strike: true
doc.p "Deleted", style: :deleted
end
styles_xml = xml_files["word/styles.xml"]
assert_includes styles_xml, "<w:strike/>"
end
def test_invalid_highlight_raises_error
assert_raises(ArgumentError) do
Notare::Style.new(:bad, highlight: "invalid_color")
end
end
def test_valid_highlight_colors
# Test a few valid highlight colors
%w[yellow red blue green cyan magenta].each do |color|
style = Notare::Style.new(:test, highlight: color)
assert_equal color, style.highlight
end
end
def test_highlight_in_text_run
xml_files = create_doc_and_read_all_xml do |doc|
doc.define_style :yellow_highlight, highlight: "yellow"
doc.p do
doc.text "Normal "
doc.text "highlighted", style: :yellow_highlight
end
end
# Highlight is in the style definition, not inline when using style reference
styles_xml = xml_files["word/styles.xml"]
assert_includes styles_xml, '<w:highlight w:val="yellow"'
document_xml = xml_files["word/document.xml"]
assert_includes document_xml, '<w:rStyle w:val="YellowHighlight"'
assert_includes document_xml, "highlighted"
end
end end

View File

@@ -3,7 +3,7 @@
require "test_helper" require "test_helper"
class TableTest < Minitest::Test class TableTest < Minitest::Test
include EzdocTestHelpers include NotareTestHelpers
def test_simple_table def test_simple_table
xml = create_doc_and_read_xml do |doc| xml = create_doc_and_read_xml do |doc|

View File

@@ -1,17 +1,17 @@
# frozen_string_literal: true # frozen_string_literal: true
$LOAD_PATH.unshift File.expand_path("../lib", __dir__) $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "ezdoc" require "notare"
require "minitest/autorun" require "minitest/autorun"
require "tempfile" require "tempfile"
require "zip" require "zip"
module EzdocTestHelpers module NotareTestHelpers
# Helper to create a document and return the document.xml content # Helper to create a document and return the document.xml content
def create_doc_and_read_xml(&block) def create_doc_and_read_xml(&block)
content = nil content = nil
Tempfile.create(["test", ".docx"]) do |file| Tempfile.create(["test", ".docx"]) do |file|
Ezdoc::Document.create(file.path, &block) Notare::Document.create(file.path, &block)
Zip::File.open(file.path) do |zip| Zip::File.open(file.path) do |zip|
content = zip.read("word/document.xml").force_encoding("UTF-8") content = zip.read("word/document.xml").force_encoding("UTF-8")
end end
@@ -23,7 +23,7 @@ module EzdocTestHelpers
def create_doc_and_read_all_xml(&block) def create_doc_and_read_all_xml(&block)
result = {} result = {}
Tempfile.create(["test", ".docx"]) do |file| Tempfile.create(["test", ".docx"]) do |file|
Ezdoc::Document.create(file.path, &block) Notare::Document.create(file.path, &block)
Zip::File.open(file.path) do |zip| Zip::File.open(file.path) do |zip|
zip.each do |entry| zip.each do |entry|
if entry.name.end_with?(".xml") || entry.name.end_with?(".rels") if entry.name.end_with?(".xml") || entry.name.end_with?(".rels")