Compare commits
9 Commits
dec346254c
...
feature/ta
| Author | SHA1 | Date | |
|---|---|---|---|
| 9a70d91fd5 | |||
| 67a60c8c6e | |||
| 52d715a6de | |||
| 75b3a163c7 | |||
| 6a54f9f8da | |||
| 597bc91c40 | |||
| 243b06d8f8 | |||
| d1819f2b64 | |||
| df8fb2fb8c |
10
.claude/settings.local.json
Normal file
10
.claude/settings.local.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"permissions": {
|
||||
"allow": [
|
||||
"Bash(bundle exec rake test:*)",
|
||||
"Bash(bundle exec rake:*)"
|
||||
],
|
||||
"deny": [],
|
||||
"ask": []
|
||||
}
|
||||
}
|
||||
231
README.md
231
README.md
@@ -58,6 +58,8 @@ Notare::Document.create("output.docx") do |doc|
|
||||
doc.i { doc.text "italic" }
|
||||
doc.text " and "
|
||||
doc.u { doc.text "underlined" }
|
||||
doc.text " and "
|
||||
doc.s { doc.text "strikethrough" }
|
||||
end
|
||||
|
||||
# Nested formatting (bold + italic)
|
||||
@@ -66,6 +68,13 @@ Notare::Document.create("output.docx") do |doc|
|
||||
doc.i { doc.text "bold and italic" }
|
||||
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
|
||||
```
|
||||
|
||||
@@ -96,6 +105,21 @@ Notare includes built-in styles and supports custom style definitions.
|
||||
|
||||
#### Built-in Styles
|
||||
|
||||
| Style | Properties |
|
||||
|-------|------------|
|
||||
| `:title` | 26pt, bold, centered |
|
||||
| `:subtitle` | 15pt, italic, gray (#666666) |
|
||||
| `:quote` | italic, gray (#666666), indented |
|
||||
| `:code` | Courier New, 10pt |
|
||||
| `:heading1` | 24pt, bold |
|
||||
| `:heading2` | 18pt, bold |
|
||||
| `:heading3` | 14pt, bold |
|
||||
| `:heading4` | 12pt, bold |
|
||||
| `:heading5` | 11pt, bold, italic |
|
||||
| `:heading6` | 10pt, bold, italic |
|
||||
|
||||
Note: `h1` through `h6` methods use the corresponding heading styles automatically.
|
||||
|
||||
```ruby
|
||||
Notare::Document.create("output.docx") do |doc|
|
||||
doc.p "This is a title", style: :title
|
||||
@@ -146,10 +170,14 @@ end
|
||||
- `bold: true/false`
|
||||
- `italic: true/false`
|
||||
- `underline: true/false`
|
||||
- `strike: true/false` - strikethrough
|
||||
- `highlight: "yellow"` - text highlight (see colors below)
|
||||
- `color: "FF0000"` (hex RGB)
|
||||
- `size: 14` (points)
|
||||
- `font: "Arial"` (font family)
|
||||
|
||||
**Highlight colors:** `black`, `blue`, `cyan`, `darkBlue`, `darkCyan`, `darkGray`, `darkGreen`, `darkMagenta`, `darkRed`, `darkYellow`, `green`, `lightGray`, `magenta`, `red`, `white`, `yellow`
|
||||
|
||||
**Paragraph properties:**
|
||||
- `align: :left / :center / :right / :justify`
|
||||
- `indent: 720` (twips, 1 inch = 1440 twips)
|
||||
@@ -182,6 +210,40 @@ Notare::Document.create("output.docx") do |doc|
|
||||
end
|
||||
```
|
||||
|
||||
#### Nested Lists
|
||||
|
||||
Lists can be nested inside list items. Mixed types (bullets inside numbered or vice versa) are supported.
|
||||
|
||||
```ruby
|
||||
Notare::Document.create("output.docx") do |doc|
|
||||
doc.ol do
|
||||
doc.li "First item"
|
||||
doc.li "Second item" do
|
||||
doc.ul do
|
||||
doc.li "Nested bullet A"
|
||||
doc.li "Nested bullet B"
|
||||
end
|
||||
end
|
||||
doc.li "Third item"
|
||||
end
|
||||
|
||||
# Deeply nested
|
||||
doc.ul do
|
||||
doc.li "Level 0"
|
||||
doc.li "Has children" do
|
||||
doc.ul do
|
||||
doc.li "Level 1"
|
||||
doc.li "Goes deeper" do
|
||||
doc.ul do
|
||||
doc.li "Level 2"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
### Tables
|
||||
|
||||
```ruby
|
||||
@@ -199,6 +261,77 @@ Notare::Document.create("output.docx") do |doc|
|
||||
end
|
||||
```
|
||||
|
||||
#### Table Styles
|
||||
|
||||
Define reusable table styles with borders, shading, cell margins, and alignment:
|
||||
|
||||
```ruby
|
||||
Notare::Document.create("output.docx") do |doc|
|
||||
# Define a custom table style
|
||||
doc.define_table_style :fancy,
|
||||
borders: { style: "double", color: "0066CC", size: 6 },
|
||||
shading: "E6F2FF",
|
||||
cell_margins: 100,
|
||||
align: :center
|
||||
|
||||
# Apply the style to a table
|
||||
doc.table(style: :fancy) do
|
||||
doc.tr do
|
||||
doc.td "Product"
|
||||
doc.td "Price"
|
||||
end
|
||||
doc.tr do
|
||||
doc.td "Widget"
|
||||
doc.td "$10.00"
|
||||
end
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
#### Table Style Properties
|
||||
|
||||
| Property | Description | Example |
|
||||
|----------|-------------|---------|
|
||||
| `borders` | Border configuration | `{ style: "single", color: "000000", size: 4 }` |
|
||||
| `shading` | Background color (hex) | `"EEEEEE"` |
|
||||
| `cell_margins` | Cell padding (twips) | `100` or `{ top: 50, bottom: 50, left: 100, right: 100 }` |
|
||||
| `align` | Table alignment | `:left`, `:center`, `:right` |
|
||||
|
||||
**Border styles:** `single`, `double`, `dotted`, `dashed`, `triple`, `none`
|
||||
|
||||
**Border configuration options:**
|
||||
|
||||
```ruby
|
||||
# All borders the same
|
||||
borders: { style: "single", color: "000000", size: 4 }
|
||||
|
||||
# Per-edge borders
|
||||
borders: {
|
||||
top: { style: "double", color: "FF0000", size: 8 },
|
||||
bottom: { style: "single", color: "000000", size: 4 },
|
||||
left: { style: "none" },
|
||||
right: { style: "none" },
|
||||
insideH: { style: "dotted", color: "CCCCCC", size: 2 },
|
||||
insideV: { style: "dotted", color: "CCCCCC", size: 2 }
|
||||
}
|
||||
|
||||
# No borders
|
||||
borders: :none
|
||||
```
|
||||
|
||||
#### Built-in Table Styles
|
||||
|
||||
| Style | Description |
|
||||
|-------|-------------|
|
||||
| `:grid` | Standard black single-line borders |
|
||||
| `:borderless` | No borders |
|
||||
|
||||
```ruby
|
||||
doc.table(style: :borderless) do
|
||||
doc.tr { doc.td "No borders here" }
|
||||
end
|
||||
```
|
||||
|
||||
### Images
|
||||
|
||||
Images can be added to paragraphs, table cells, and list items. Supports PNG and JPEG formats.
|
||||
@@ -246,6 +379,91 @@ Notare::Document.create("output.docx") do |doc|
|
||||
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
|
||||
|
||||
```ruby
|
||||
@@ -296,12 +514,19 @@ end
|
||||
| `b { }` | Bold formatting |
|
||||
| `i { }` | Italic 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 |
|
||||
| `ul { }` | Bullet list |
|
||||
| `ol { }` | Numbered list |
|
||||
| `define_table_style(name, **props)` | Define a custom table style |
|
||||
| `ul { }` | Bullet list (can be nested) |
|
||||
| `ol { }` | Numbered list (can be nested) |
|
||||
| `li(text)` | List item with text |
|
||||
| `li { }` | List item with block content |
|
||||
| `table { }` | Table |
|
||||
| `li(text) { }` | List item with text and nested content |
|
||||
| `table(style:) { }` | Table with optional style |
|
||||
| `tr { }` | Table row |
|
||||
| `td(text)` | Table cell with text |
|
||||
| `td { }` | Table cell with block content |
|
||||
|
||||
250
examples/full_demo.rb
Executable file → Normal file
250
examples/full_demo.rb
Executable file → Normal file
@@ -1,4 +1,3 @@
|
||||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Full demo of all Notare features
|
||||
@@ -16,6 +15,27 @@ Notare::Document.create(OUTPUT_FILE) do |doc|
|
||||
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
|
||||
doc.define_style :deleted_text, strike: true, color: "999999"
|
||||
doc.define_style :important, highlight: "yellow", bold: true
|
||||
|
||||
# ============================================================================
|
||||
# Custom Table Styles
|
||||
# ============================================================================
|
||||
doc.define_table_style :fancy_table,
|
||||
borders: { style: "double", color: "0066CC", size: 6 },
|
||||
shading: "E6F2FF",
|
||||
cell_margins: 80,
|
||||
align: :center
|
||||
|
||||
doc.define_table_style :minimal_table,
|
||||
borders: {
|
||||
top: { style: "single", color: "CCCCCC", size: 4 },
|
||||
bottom: { style: "single", color: "CCCCCC", size: 4 },
|
||||
left: { style: "none" },
|
||||
right: { style: "none" },
|
||||
insideH: { style: "dotted", color: "DDDDDD", size: 2 },
|
||||
insideV: { style: "none" }
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Title and Introduction
|
||||
@@ -34,6 +54,8 @@ Notare::Document.create(OUTPUT_FILE) do |doc|
|
||||
doc.i { doc.text "italic" }
|
||||
doc.text ", "
|
||||
doc.u { doc.text "underlined" }
|
||||
doc.text ", "
|
||||
doc.s { doc.text "strikethrough" }
|
||||
doc.text ", and "
|
||||
doc.b do
|
||||
doc.i do
|
||||
@@ -43,6 +65,13 @@ Notare::Document.create(OUTPUT_FILE) do |doc|
|
||||
doc.text " formatting."
|
||||
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
|
||||
# ============================================================================
|
||||
@@ -74,11 +103,54 @@ Notare::Document.create(OUTPUT_FILE) do |doc|
|
||||
doc.text " in one paragraph."
|
||||
end
|
||||
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.ul do
|
||||
@@ -98,10 +170,75 @@ Notare::Document.create(OUTPUT_FILE) do |doc|
|
||||
doc.li "Step three"
|
||||
end
|
||||
|
||||
doc.h3 "Nested Lists"
|
||||
doc.ol do
|
||||
doc.li "Main topic one"
|
||||
doc.li "Main topic two" do
|
||||
doc.ul do
|
||||
doc.li "Supporting point A"
|
||||
doc.li "Supporting point B" do
|
||||
doc.ul do
|
||||
doc.li "Detail 1"
|
||||
doc.li "Detail 2"
|
||||
end
|
||||
end
|
||||
doc.li "Supporting point C"
|
||||
end
|
||||
end
|
||||
doc.li "Main topic three"
|
||||
end
|
||||
|
||||
doc.p "Mixed nested lists with formatting:"
|
||||
doc.ul do
|
||||
doc.li do
|
||||
doc.b { doc.text "Bold parent item" }
|
||||
end
|
||||
doc.li "Item with nested numbered list" do
|
||||
doc.ol do
|
||||
doc.li "First sub-step"
|
||||
doc.li "Second sub-step"
|
||||
end
|
||||
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.h3 "Default Table"
|
||||
doc.table do
|
||||
doc.tr do
|
||||
doc.td { doc.b { doc.text "Feature" } }
|
||||
@@ -116,7 +253,27 @@ Notare::Document.create(OUTPUT_FILE) do |doc|
|
||||
doc.tr do
|
||||
doc.td "Formatting"
|
||||
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
|
||||
doc.tr do
|
||||
doc.td "Headings"
|
||||
@@ -133,12 +290,63 @@ Notare::Document.create(OUTPUT_FILE) do |doc|
|
||||
doc.td { doc.text "Complete", style: :success }
|
||||
doc.td "PNG and JPEG"
|
||||
end
|
||||
doc.tr do
|
||||
doc.td "Nested Lists"
|
||||
doc.td { doc.text "Complete", style: :success }
|
||||
doc.td "Multi-level with mixed types"
|
||||
end
|
||||
end
|
||||
|
||||
doc.h3 "Styled Tables"
|
||||
|
||||
doc.p "Fancy table with double borders and shading:"
|
||||
doc.table(style: :fancy_table) do
|
||||
doc.tr do
|
||||
doc.td { doc.b { doc.text "Product" } }
|
||||
doc.td { doc.b { doc.text "Price" } }
|
||||
doc.td { doc.b { doc.text "Quantity" } }
|
||||
end
|
||||
doc.tr do
|
||||
doc.td "Widget A"
|
||||
doc.td "$10.00"
|
||||
doc.td "100"
|
||||
end
|
||||
doc.tr do
|
||||
doc.td "Widget B"
|
||||
doc.td "$15.00"
|
||||
doc.td "50"
|
||||
end
|
||||
end
|
||||
|
||||
doc.p "Minimal table with horizontal lines only:"
|
||||
doc.table(style: :minimal_table) do
|
||||
doc.tr do
|
||||
doc.td { doc.b { doc.text "Name" } }
|
||||
doc.td { doc.b { doc.text "Role" } }
|
||||
end
|
||||
doc.tr do
|
||||
doc.td "Alice"
|
||||
doc.td "Developer"
|
||||
end
|
||||
doc.tr do
|
||||
doc.td "Bob"
|
||||
doc.td "Designer"
|
||||
end
|
||||
end
|
||||
|
||||
doc.p "Borderless table (built-in style):"
|
||||
doc.table(style: :borderless) do
|
||||
doc.tr do
|
||||
doc.td "No"
|
||||
doc.td "borders"
|
||||
doc.td "here"
|
||||
end
|
||||
end
|
||||
|
||||
# ============================================================================
|
||||
# 7. Images
|
||||
# 10. Images
|
||||
# ============================================================================
|
||||
doc.h2 "7. Images"
|
||||
doc.h2 "10. Images"
|
||||
|
||||
doc.p "Image with explicit dimensions:"
|
||||
doc.p do
|
||||
@@ -163,9 +371,19 @@ Notare::Document.create(OUTPUT_FILE) do |doc|
|
||||
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.text "This final paragraph combines "
|
||||
doc.b { doc.text "multiple" }
|
||||
@@ -173,9 +391,21 @@ Notare::Document.create(OUTPUT_FILE) do |doc|
|
||||
doc.i { doc.text "formatting" }
|
||||
doc.text " options with "
|
||||
doc.text "custom styles", style: :highlight
|
||||
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
|
||||
|
||||
doc.p "End of demo document.", style: :centered_large
|
||||
end
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@ 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"
|
||||
@@ -14,6 +16,7 @@ require_relative "notare/nodes/table_row"
|
||||
require_relative "notare/nodes/table_cell"
|
||||
require_relative "notare/image_dimensions"
|
||||
require_relative "notare/style"
|
||||
require_relative "notare/table_style"
|
||||
require_relative "notare/xml/content_types"
|
||||
require_relative "notare/xml/relationships"
|
||||
require_relative "notare/xml/document_xml"
|
||||
|
||||
@@ -60,6 +60,30 @@ module Notare
|
||||
with_format(:underline, &block)
|
||||
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)
|
||||
list(:bullet, &block)
|
||||
end
|
||||
@@ -69,17 +93,15 @@ module Notare
|
||||
end
|
||||
|
||||
def li(text = nil, &block)
|
||||
item = Nodes::ListItem.new([], list_type: @current_list.type, num_id: @current_list.num_id)
|
||||
if block
|
||||
with_target(item, &block)
|
||||
elsif text
|
||||
item.add_run(Nodes::Run.new(text, **current_formatting))
|
||||
end
|
||||
current_type = @list_type_stack.last
|
||||
item = Nodes::ListItem.new([], list_type: current_type, num_id: @current_list.num_id, level: @list_level)
|
||||
item.add_run(Nodes::Run.new(text, **current_formatting)) if text
|
||||
with_target(item, &block) if block
|
||||
@current_list.add_item(item)
|
||||
end
|
||||
|
||||
def table(&block)
|
||||
tbl = Nodes::Table.new
|
||||
def table(style: nil, &block)
|
||||
tbl = Nodes::Table.new(style: resolve_table_style(style))
|
||||
previous_table = @current_table
|
||||
@current_table = tbl
|
||||
block.call
|
||||
@@ -110,14 +132,31 @@ module Notare
|
||||
|
||||
def list(type, &block)
|
||||
@num_id_counter ||= 0
|
||||
@num_id_counter += 1
|
||||
@list_level ||= 0
|
||||
@list_type_stack ||= []
|
||||
|
||||
list_node = Nodes::List.new(type: type, num_id: @num_id_counter)
|
||||
previous_list = @current_list
|
||||
@current_list = list_node
|
||||
block.call
|
||||
@current_list = previous_list
|
||||
@nodes << list_node
|
||||
nested = !previous_list.nil?
|
||||
|
||||
if nested
|
||||
# Nested list: reuse parent list, push new type, increment level
|
||||
@list_level += 1
|
||||
@list_type_stack.push(type)
|
||||
block.call
|
||||
@list_type_stack.pop
|
||||
@list_level -= 1
|
||||
else
|
||||
# Top-level list: new List node
|
||||
@num_id_counter += 1
|
||||
mark_has_lists!
|
||||
list_node = Nodes::List.new(type: type, num_id: @num_id_counter)
|
||||
@list_type_stack.push(type)
|
||||
@current_list = list_node
|
||||
block.call
|
||||
@current_list = previous_list
|
||||
@list_type_stack.pop
|
||||
@nodes << list_node
|
||||
end
|
||||
end
|
||||
|
||||
def with_format(format, &block)
|
||||
@@ -139,7 +178,8 @@ module Notare
|
||||
{
|
||||
bold: @format_stack.include?(:bold),
|
||||
italic: @format_stack.include?(:italic),
|
||||
underline: @format_stack.include?(:underline)
|
||||
underline: @format_stack.include?(:underline),
|
||||
strike: @format_stack.include?(:strike)
|
||||
}
|
||||
end
|
||||
|
||||
@@ -158,5 +198,12 @@ module Notare
|
||||
|
||||
style(style_or_name) || raise(ArgumentError, "Unknown style: #{style_or_name}")
|
||||
end
|
||||
|
||||
def resolve_table_style(style_or_name)
|
||||
return nil if style_or_name.nil?
|
||||
return style_or_name if style_or_name.is_a?(TableStyle)
|
||||
|
||||
table_style(style_or_name) || raise(ArgumentError, "Unknown table style: #{style_or_name}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ module Notare
|
||||
class Document
|
||||
include Builder
|
||||
|
||||
attr_reader :nodes, :styles
|
||||
attr_reader :nodes, :styles, :table_styles, :hyperlinks
|
||||
|
||||
def self.create(path, &block)
|
||||
doc = new
|
||||
@@ -21,9 +21,13 @@ module Notare
|
||||
@current_table = nil
|
||||
@current_row = nil
|
||||
@num_id_counter = 0
|
||||
@has_lists = false
|
||||
@images = {}
|
||||
@hyperlinks = []
|
||||
@styles = {}
|
||||
@table_styles = {}
|
||||
register_built_in_styles
|
||||
register_built_in_table_styles
|
||||
end
|
||||
|
||||
def define_style(name, **properties)
|
||||
@@ -34,6 +38,14 @@ module Notare
|
||||
@styles[name]
|
||||
end
|
||||
|
||||
def define_table_style(name, **properties)
|
||||
@table_styles[name] = TableStyle.new(name, **properties)
|
||||
end
|
||||
|
||||
def table_style(name)
|
||||
@table_styles[name]
|
||||
end
|
||||
|
||||
def save(path)
|
||||
Package.new(self).save(path)
|
||||
end
|
||||
@@ -42,10 +54,25 @@ module Notare
|
||||
@nodes.select { |n| n.is_a?(Nodes::List) }
|
||||
end
|
||||
|
||||
def uses_lists?
|
||||
@has_lists
|
||||
end
|
||||
|
||||
def mark_has_lists!
|
||||
@has_lists = true
|
||||
end
|
||||
|
||||
def images
|
||||
@images.values
|
||||
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)
|
||||
return @images[path] if @images[path]
|
||||
|
||||
@@ -61,11 +88,17 @@ module Notare
|
||||
def next_image_rid
|
||||
# rId1 = styles.xml (always present)
|
||||
# rId2 = numbering.xml (if lists present)
|
||||
# rId3+ = images
|
||||
base = lists.any? ? 3 : 2
|
||||
# rId3+ = images, then hyperlinks
|
||||
base = @has_lists ? 3 : 2
|
||||
"rId#{base + @images.size}"
|
||||
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
|
||||
# Headings (spacing_before ensures they're rendered as paragraph styles)
|
||||
define_style :heading1, size: 24, bold: true, spacing_before: 240, spacing_after: 120
|
||||
@@ -81,5 +114,13 @@ module Notare
|
||||
define_style :quote, italic: true, color: "666666", indent: 720
|
||||
define_style :code, font: "Courier New", size: 10
|
||||
end
|
||||
|
||||
def register_built_in_table_styles
|
||||
define_table_style :grid,
|
||||
borders: { style: "single", color: "000000", size: 4 }
|
||||
|
||||
define_table_style :borderless,
|
||||
borders: :none
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
18
lib/notare/nodes/break.rb
Normal file
18
lib/notare/nodes/break.rb
Normal 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
|
||||
20
lib/notare/nodes/hyperlink.rb
Normal file
20
lib/notare/nodes/hyperlink.rb
Normal 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
|
||||
@@ -3,13 +3,14 @@
|
||||
module Notare
|
||||
module Nodes
|
||||
class ListItem < Base
|
||||
attr_reader :runs, :list_type, :num_id
|
||||
attr_reader :runs, :list_type, :num_id, :level
|
||||
|
||||
def initialize(runs = [], list_type:, num_id:)
|
||||
def initialize(runs = [], list_type:, num_id:, level: 0)
|
||||
super()
|
||||
@runs = runs
|
||||
@list_type = list_type
|
||||
@num_id = num_id
|
||||
@level = level
|
||||
end
|
||||
|
||||
def add_run(run)
|
||||
|
||||
@@ -3,14 +3,18 @@
|
||||
module Notare
|
||||
module Nodes
|
||||
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()
|
||||
@text = text
|
||||
@bold = bold
|
||||
@italic = italic
|
||||
@underline = underline
|
||||
@strike = strike
|
||||
@highlight = highlight
|
||||
@color = color
|
||||
@style = style
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,11 +3,12 @@
|
||||
module Notare
|
||||
module Nodes
|
||||
class Table < Base
|
||||
attr_reader :rows
|
||||
attr_reader :rows, :style
|
||||
|
||||
def initialize
|
||||
super
|
||||
def initialize(style: nil)
|
||||
super()
|
||||
@rows = []
|
||||
@style = style
|
||||
end
|
||||
|
||||
def add_row(row)
|
||||
|
||||
@@ -29,13 +29,17 @@ module Notare
|
||||
private
|
||||
|
||||
def lists?
|
||||
@document.lists.any?
|
||||
@document.uses_lists?
|
||||
end
|
||||
|
||||
def images
|
||||
@document.images
|
||||
end
|
||||
|
||||
def hyperlinks
|
||||
@document.hyperlinks
|
||||
end
|
||||
|
||||
def content_types_xml
|
||||
Xml::ContentTypes.new(has_numbering: lists?, images: images, has_styles: true).to_xml
|
||||
end
|
||||
@@ -45,7 +49,9 @@ module Notare
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def document_xml
|
||||
@@ -53,7 +59,7 @@ module Notare
|
||||
end
|
||||
|
||||
def styles_xml
|
||||
Xml::StylesXml.new(@document.styles).to_xml
|
||||
Xml::StylesXml.new(@document.styles, @document.table_styles).to_xml
|
||||
end
|
||||
|
||||
def numbering_xml
|
||||
|
||||
@@ -2,18 +2,24 @@
|
||||
|
||||
module Notare
|
||||
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
|
||||
|
||||
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,
|
||||
size: nil, font: nil, align: nil, indent: nil,
|
||||
spacing_before: nil, spacing_after: nil)
|
||||
def initialize(name, bold: nil, italic: nil, underline: nil, strike: nil,
|
||||
highlight: 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
|
||||
@strike = strike
|
||||
@highlight = validate_highlight(highlight)
|
||||
@color = normalize_color(color)
|
||||
@size = size
|
||||
@font = font
|
||||
@@ -36,7 +42,7 @@ module Notare
|
||||
end
|
||||
|
||||
def text_properties?
|
||||
!!(bold || italic || underline || color || size || font)
|
||||
!!(bold || italic || underline || strike || highlight || color || size || font)
|
||||
end
|
||||
|
||||
# Size in half-points for OOXML (14pt = 28 half-points)
|
||||
@@ -61,5 +67,14 @@ module Notare
|
||||
|
||||
raise ArgumentError, "Invalid alignment: #{align}. Use #{ALIGNMENTS.join(", ")}"
|
||||
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
|
||||
|
||||
83
lib/notare/table_style.rb
Normal file
83
lib/notare/table_style.rb
Normal file
@@ -0,0 +1,83 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
class TableStyle
|
||||
attr_reader :name, :borders, :shading, :cell_margins, :align
|
||||
|
||||
BORDER_STYLES = %w[single double dotted dashed triple none nil].freeze
|
||||
BORDER_POSITIONS = %i[top bottom left right insideH insideV].freeze
|
||||
ALIGNMENTS = %i[left center right].freeze
|
||||
|
||||
def initialize(name, borders: nil, shading: nil, cell_margins: nil, align: nil)
|
||||
@name = name
|
||||
@borders = normalize_borders(borders)
|
||||
@shading = normalize_color(shading)
|
||||
@cell_margins = normalize_cell_margins(cell_margins)
|
||||
@align = validate_align(align)
|
||||
end
|
||||
|
||||
def style_id
|
||||
name.to_s.split("_").map(&:capitalize).join
|
||||
end
|
||||
|
||||
def display_name
|
||||
name.to_s.split("_").map(&:capitalize).join(" ")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def normalize_borders(borders)
|
||||
return nil if borders.nil?
|
||||
return :none if borders == :none
|
||||
|
||||
# Check if it's a per-edge configuration
|
||||
if borders.keys.any? { |k| BORDER_POSITIONS.include?(k) }
|
||||
borders.transform_values { |v| normalize_single_border(v) }
|
||||
else
|
||||
# Single border config applied to all edges
|
||||
normalize_single_border(borders)
|
||||
end
|
||||
end
|
||||
|
||||
def normalize_single_border(border)
|
||||
return :none if border == :none || border[:style] == "none"
|
||||
|
||||
style = border[:style] || "single"
|
||||
unless BORDER_STYLES.include?(style)
|
||||
raise ArgumentError, "Invalid border style: #{style}. Use #{BORDER_STYLES.join(", ")}"
|
||||
end
|
||||
|
||||
{
|
||||
style: style,
|
||||
color: normalize_color(border[:color]) || "000000",
|
||||
size: border[:size] || 4
|
||||
}
|
||||
end
|
||||
|
||||
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 normalize_cell_margins(margins)
|
||||
return nil if margins.nil?
|
||||
|
||||
if margins.is_a?(Hash)
|
||||
margins.slice(:top, :bottom, :left, :right)
|
||||
else
|
||||
margins.to_i
|
||||
end
|
||||
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
|
||||
@@ -1,5 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Notare
|
||||
VERSION = "0.0.1"
|
||||
VERSION = "0.0.3"
|
||||
end
|
||||
|
||||
@@ -37,6 +37,16 @@ module Notare
|
||||
render_list(xml, node)
|
||||
when Nodes::Table
|
||||
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
|
||||
|
||||
@@ -59,7 +69,7 @@ module Notare
|
||||
xml["w"].p do
|
||||
xml["w"].pPr do
|
||||
xml["w"].numPr do
|
||||
xml["w"].ilvl("w:val" => "0")
|
||||
xml["w"].ilvl("w:val" => item.level.to_s)
|
||||
xml["w"].numId("w:val" => item.num_id.to_s)
|
||||
end
|
||||
end
|
||||
@@ -71,19 +81,42 @@ module Notare
|
||||
case run
|
||||
when Nodes::Image
|
||||
render_image(xml, run)
|
||||
when Nodes::Break
|
||||
render_break(xml, run)
|
||||
when Nodes::Hyperlink
|
||||
render_hyperlink(xml, run)
|
||||
when Nodes::Run
|
||||
render_text_run(xml, run)
|
||||
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)
|
||||
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"].rStyle("w:val" => run.style.style_id) if run.style
|
||||
xml["w"].b if run.bold
|
||||
xml["w"].i if run.italic
|
||||
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
|
||||
xml["w"].t(run.text, "xml:space" => "preserve")
|
||||
@@ -136,9 +169,13 @@ module Notare
|
||||
xml["w"].tbl do
|
||||
xml["w"].tblPr do
|
||||
xml["w"].tblW("w:w" => "5000", "w:type" => "pct")
|
||||
xml["w"].tblBorders do
|
||||
%w[top left bottom right insideH insideV].each do |border|
|
||||
xml["w"].send(border, "w:val" => "single", "w:sz" => "4", "w:space" => "0", "w:color" => "000000")
|
||||
if table.style
|
||||
xml["w"].tblStyle("w:val" => table.style.style_id)
|
||||
else
|
||||
xml["w"].tblBorders do
|
||||
%w[top left bottom right insideH insideV].each do |border|
|
||||
xml["w"].send(border, "w:val" => "single", "w:sz" => "4", "w:space" => "0", "w:color" => "000000")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,6 +4,8 @@ module Notare
|
||||
module Xml
|
||||
class Numbering
|
||||
NAMESPACE = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
||||
BULLET_CHARS = ["•", "○", "■"].freeze
|
||||
NUMBER_FORMATS = %w[decimal lowerLetter lowerRoman].freeze
|
||||
|
||||
def initialize(lists)
|
||||
@lists = lists
|
||||
@@ -28,13 +30,16 @@ module Notare
|
||||
|
||||
def render_abstract_num(xml, list)
|
||||
xml["w"].abstractNum("w:abstractNumId" => list.num_id.to_s) do
|
||||
xml["w"].lvl("w:ilvl" => "0") do
|
||||
xml["w"].start("w:val" => "1")
|
||||
xml["w"].numFmt("w:val" => num_format(list.type))
|
||||
xml["w"].lvlText("w:val" => lvl_text(list.type))
|
||||
xml["w"].lvlJc("w:val" => "left")
|
||||
xml["w"].pPr do
|
||||
xml["w"].ind("w:left" => "720", "w:hanging" => "360")
|
||||
9.times do |level|
|
||||
xml["w"].lvl("w:ilvl" => level.to_s) do
|
||||
xml["w"].start("w:val" => "1")
|
||||
xml["w"].numFmt("w:val" => num_format_for_level(list.type, level))
|
||||
xml["w"].lvlText("w:val" => lvl_text_for_level(list.type, level))
|
||||
xml["w"].lvlJc("w:val" => "left")
|
||||
xml["w"].pPr do
|
||||
left = 720 * (level + 1)
|
||||
xml["w"].ind("w:left" => left.to_s, "w:hanging" => "360")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -46,12 +51,20 @@ module Notare
|
||||
end
|
||||
end
|
||||
|
||||
def num_format(type)
|
||||
type == :bullet ? "bullet" : "decimal"
|
||||
def num_format_for_level(type, level)
|
||||
if type == :bullet
|
||||
"bullet"
|
||||
else
|
||||
NUMBER_FORMATS[level % NUMBER_FORMATS.length]
|
||||
end
|
||||
end
|
||||
|
||||
def lvl_text(type)
|
||||
type == :bullet ? "•" : "%1."
|
||||
def lvl_text_for_level(type, level)
|
||||
if type == :bullet
|
||||
BULLET_CHARS[level % BULLET_CHARS.length]
|
||||
else
|
||||
"%#{level + 1}."
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -24,10 +24,12 @@ module Notare
|
||||
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"
|
||||
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
|
||||
@images = images
|
||||
@hyperlinks = hyperlinks
|
||||
@has_styles = has_styles
|
||||
end
|
||||
|
||||
@@ -60,6 +62,16 @@ module Notare
|
||||
Target: "media/#{image.filename}"
|
||||
)
|
||||
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
|
||||
builder.to_xml
|
||||
|
||||
@@ -12,8 +12,15 @@ module Notare
|
||||
justify: "both"
|
||||
}.freeze
|
||||
|
||||
def initialize(styles)
|
||||
TABLE_ALIGNMENT_MAP = {
|
||||
left: "left",
|
||||
center: "center",
|
||||
right: "right"
|
||||
}.freeze
|
||||
|
||||
def initialize(styles, table_styles = {})
|
||||
@styles = styles
|
||||
@table_styles = table_styles
|
||||
end
|
||||
|
||||
def to_xml
|
||||
@@ -24,6 +31,10 @@ module Notare
|
||||
@styles.each_value do |style|
|
||||
render_style(xml, style)
|
||||
end
|
||||
|
||||
@table_styles.each_value do |style|
|
||||
render_table_style(xml, style)
|
||||
end
|
||||
end
|
||||
end
|
||||
builder.to_xml
|
||||
@@ -59,6 +70,61 @@ module Notare
|
||||
xml["w"].b if style.bold
|
||||
xml["w"].i if style.italic
|
||||
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
|
||||
|
||||
def render_table_style(xml, style)
|
||||
xml["w"].style("w:type" => "table", "w:styleId" => style.style_id) do
|
||||
xml["w"].name("w:val" => style.display_name)
|
||||
|
||||
xml["w"].tblPr do
|
||||
render_table_borders(xml, style.borders) if style.borders
|
||||
render_table_shading(xml, style.shading) if style.shading
|
||||
render_table_cell_margins(xml, style.cell_margins) if style.cell_margins
|
||||
xml["w"].jc("w:val" => TABLE_ALIGNMENT_MAP[style.align]) if style.align
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def render_table_borders(xml, borders)
|
||||
xml["w"].tblBorders do
|
||||
%i[top left bottom right insideH insideV].each do |pos|
|
||||
border = borders == :none ? :none : (borders[pos] || borders)
|
||||
render_single_border(xml, pos, border)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def render_single_border(xml, position, border)
|
||||
if border == :none
|
||||
xml["w"].send(position, "w:val" => "nil")
|
||||
else
|
||||
xml["w"].send(position,
|
||||
"w:val" => border[:style],
|
||||
"w:sz" => border[:size].to_s,
|
||||
"w:space" => "0",
|
||||
"w:color" => border[:color])
|
||||
end
|
||||
end
|
||||
|
||||
def render_table_shading(xml, color)
|
||||
xml["w"].shd("w:val" => "clear", "w:color" => "auto", "w:fill" => color)
|
||||
end
|
||||
|
||||
def render_table_cell_margins(xml, margins)
|
||||
xml["w"].tblCellMar do
|
||||
if margins.is_a?(Hash)
|
||||
xml["w"].top("w:w" => margins[:top].to_s, "w:type" => "dxa") if margins[:top]
|
||||
xml["w"].left("w:w" => margins[:left].to_s, "w:type" => "dxa") if margins[:left]
|
||||
xml["w"].bottom("w:w" => margins[:bottom].to_s, "w:type" => "dxa") if margins[:bottom]
|
||||
xml["w"].right("w:w" => margins[:right].to_s, "w:type" => "dxa") if margins[:right]
|
||||
else
|
||||
%i[top left bottom right].each do |side|
|
||||
xml["w"].send(side, "w:w" => margins.to_s, "w:type" => "dxa")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
|
||||
spec.authors = ["Mathias"]
|
||||
spec.summary = "A Ruby gem for creating docx files with a simple DSL"
|
||||
spec.description = "Notare provides a clean DSL for creating Word documents in Ruby"
|
||||
spec.homepage = "https://github.com/mathias/notare"
|
||||
spec.homepage = "https://git.kaukus.no/Kaukus/Notare"
|
||||
spec.license = "MIT"
|
||||
spec.required_ruby_version = ">= 3.0.0"
|
||||
|
||||
|
||||
@@ -133,4 +133,49 @@ class FormattingTest < Minitest::Test
|
||||
assert_includes xml, "bold+italic "
|
||||
assert_includes xml, "all three"
|
||||
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
|
||||
|
||||
127
test/hyperlink_test.rb
Normal file
127
test/hyperlink_test.rb
Normal 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
|
||||
85
test/line_break_test.rb
Normal file
85
test/line_break_test.rb
Normal 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
|
||||
@@ -144,4 +144,98 @@ class ListTest < Minitest::Test
|
||||
|
||||
refute xml_files.key?("word/numbering.xml"), "numbering.xml should not exist without lists"
|
||||
end
|
||||
|
||||
#
|
||||
# Nested List Tests
|
||||
#
|
||||
|
||||
def test_nested_bullet_list
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.ul do
|
||||
doc.li "Parent item"
|
||||
doc.li "Item with nested list" do
|
||||
doc.ul do
|
||||
doc.li "Nested item 1"
|
||||
doc.li "Nested item 2"
|
||||
end
|
||||
end
|
||||
doc.li "Another parent item"
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "Parent item"
|
||||
assert_includes xml, "Nested item 1"
|
||||
assert_includes xml, "Nested item 2"
|
||||
assert_includes xml, "Another parent item"
|
||||
# Check for level 0 and level 1
|
||||
assert_includes xml, 'w:ilvl w:val="0"'
|
||||
assert_includes xml, 'w:ilvl w:val="1"'
|
||||
end
|
||||
|
||||
def test_nested_numbered_list
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.ol do
|
||||
doc.li "First"
|
||||
doc.li "Second with nested" do
|
||||
doc.ol do
|
||||
doc.li "Nested 1"
|
||||
doc.li "Nested 2"
|
||||
end
|
||||
end
|
||||
doc.li "Third"
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "First"
|
||||
assert_includes xml, "Nested 1"
|
||||
assert_includes xml, "Third"
|
||||
assert_includes xml, 'w:ilvl w:val="0"'
|
||||
assert_includes xml, 'w:ilvl w:val="1"'
|
||||
end
|
||||
|
||||
def test_mixed_nested_list
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.ol do
|
||||
doc.li "Numbered item 1"
|
||||
doc.li "Numbered item 2" do
|
||||
doc.ul do
|
||||
doc.li "Bullet inside numbered"
|
||||
end
|
||||
end
|
||||
doc.li "Numbered item 3"
|
||||
end
|
||||
end
|
||||
|
||||
doc_xml = xml_files["word/document.xml"]
|
||||
assert_includes doc_xml, "Numbered item 1"
|
||||
assert_includes doc_xml, "Bullet inside numbered"
|
||||
assert_includes doc_xml, "Numbered item 3"
|
||||
assert_includes doc_xml, 'w:ilvl w:val="0"'
|
||||
assert_includes doc_xml, 'w:ilvl w:val="1"'
|
||||
end
|
||||
|
||||
def test_deeply_nested_list
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.ul do
|
||||
doc.li "Level 0"
|
||||
doc.li "Has nested" do
|
||||
doc.ul do
|
||||
doc.li "Level 1"
|
||||
doc.li "Has deeper nested" do
|
||||
doc.ul do
|
||||
doc.li "Level 2"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "Level 0"
|
||||
assert_includes xml, "Level 1"
|
||||
assert_includes xml, "Level 2"
|
||||
assert_includes xml, 'w:ilvl w:val="0"'
|
||||
assert_includes xml, 'w:ilvl w:val="1"'
|
||||
assert_includes xml, 'w:ilvl w:val="2"'
|
||||
end
|
||||
end
|
||||
|
||||
54
test/page_break_test.rb
Normal file
54
test/page_break_test.rb
Normal 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
|
||||
@@ -168,4 +168,56 @@ class StyleTest < Minitest::Test
|
||||
assert_includes styles_xml, 'w:ascii="Arial"'
|
||||
assert_includes styles_xml, '<w:jc w:val="center"'
|
||||
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
|
||||
|
||||
224
test/table_style_test.rb
Normal file
224
test/table_style_test.rb
Normal file
@@ -0,0 +1,224 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
class TableStyleTest < Minitest::Test
|
||||
include NotareTestHelpers
|
||||
|
||||
# --- TableStyle class tests ---
|
||||
|
||||
def test_table_style_id_generation
|
||||
style = Notare::TableStyle.new(:my_table_style)
|
||||
assert_equal "MyTableStyle", style.style_id
|
||||
end
|
||||
|
||||
def test_table_style_display_name
|
||||
style = Notare::TableStyle.new(:my_table_style)
|
||||
assert_equal "My Table Style", style.display_name
|
||||
end
|
||||
|
||||
def test_invalid_border_style_raises_error
|
||||
assert_raises(ArgumentError) do
|
||||
Notare::TableStyle.new(:bad, borders: { style: "invalid" })
|
||||
end
|
||||
end
|
||||
|
||||
def test_invalid_color_raises_error
|
||||
assert_raises(ArgumentError) do
|
||||
Notare::TableStyle.new(:bad, shading: "invalid")
|
||||
end
|
||||
end
|
||||
|
||||
def test_color_normalizes_hash_prefix
|
||||
style = Notare::TableStyle.new(:test, shading: "#ff0000")
|
||||
assert_equal "FF0000", style.shading
|
||||
end
|
||||
|
||||
def test_invalid_alignment_raises_error
|
||||
assert_raises(ArgumentError) do
|
||||
Notare::TableStyle.new(:bad, align: :invalid)
|
||||
end
|
||||
end
|
||||
|
||||
# --- Document registration tests ---
|
||||
|
||||
def test_define_table_style
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.define_table_style :custom, borders: { style: "double", color: "FF0000", size: 8 }
|
||||
doc.table(style: :custom) do
|
||||
doc.tr { doc.td "Test" }
|
||||
end
|
||||
end
|
||||
|
||||
styles_xml = xml_files["word/styles.xml"]
|
||||
assert_includes styles_xml, 'w:styleId="Custom"'
|
||||
assert_includes styles_xml, 'w:type="table"'
|
||||
assert_includes styles_xml, 'w:val="double"'
|
||||
assert_includes styles_xml, 'w:color="FF0000"'
|
||||
end
|
||||
|
||||
def test_unknown_table_style_raises_error
|
||||
assert_raises(ArgumentError) do
|
||||
Tempfile.create(["test", ".docx"]) do |file|
|
||||
Notare::Document.create(file.path) do |doc|
|
||||
doc.table(style: :nonexistent) { doc.tr { doc.td "Test" } }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# --- Style application tests ---
|
||||
|
||||
def test_table_with_style_reference
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.define_table_style :bordered, borders: { style: "single", color: "000000", size: 4 }
|
||||
doc.table(style: :bordered) do
|
||||
doc.tr { doc.td "Cell" }
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, '<w:tblStyle w:val="Bordered"'
|
||||
end
|
||||
|
||||
def test_table_without_style_uses_default_borders
|
||||
xml = create_doc_and_read_xml do |doc|
|
||||
doc.table do
|
||||
doc.tr { doc.td "Cell" }
|
||||
end
|
||||
end
|
||||
|
||||
assert_includes xml, "<w:tblBorders>"
|
||||
refute_includes xml, "<w:tblStyle"
|
||||
end
|
||||
|
||||
def test_borderless_table_style
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.define_table_style :no_borders, borders: :none
|
||||
doc.table(style: :no_borders) do
|
||||
doc.tr { doc.td "Cell" }
|
||||
end
|
||||
end
|
||||
|
||||
styles_xml = xml_files["word/styles.xml"]
|
||||
assert_includes styles_xml, 'w:val="nil"'
|
||||
end
|
||||
|
||||
def test_table_style_with_shading
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.define_table_style :shaded, shading: "EEEEEE"
|
||||
doc.table(style: :shaded) do
|
||||
doc.tr { doc.td "Cell" }
|
||||
end
|
||||
end
|
||||
|
||||
styles_xml = xml_files["word/styles.xml"]
|
||||
assert_includes styles_xml, 'w:fill="EEEEEE"'
|
||||
end
|
||||
|
||||
def test_table_style_with_cell_margins
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.define_table_style :padded, cell_margins: 100
|
||||
doc.table(style: :padded) do
|
||||
doc.tr { doc.td "Cell" }
|
||||
end
|
||||
end
|
||||
|
||||
styles_xml = xml_files["word/styles.xml"]
|
||||
assert_includes styles_xml, "<w:tblCellMar>"
|
||||
assert_includes styles_xml, 'w:w="100"'
|
||||
end
|
||||
|
||||
def test_table_style_with_alignment
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.define_table_style :centered, align: :center
|
||||
doc.table(style: :centered) do
|
||||
doc.tr { doc.td "Cell" }
|
||||
end
|
||||
end
|
||||
|
||||
styles_xml = xml_files["word/styles.xml"]
|
||||
assert_includes styles_xml, '<w:jc w:val="center"'
|
||||
end
|
||||
|
||||
def test_combined_table_style_properties
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.define_table_style :fancy,
|
||||
borders: { style: "double", color: "0000FF", size: 8 },
|
||||
shading: "F0F0F0",
|
||||
cell_margins: { top: 50, bottom: 50, left: 100, right: 100 },
|
||||
align: :center
|
||||
|
||||
doc.table(style: :fancy) do
|
||||
doc.tr { doc.td "Fancy" }
|
||||
end
|
||||
end
|
||||
|
||||
styles_xml = xml_files["word/styles.xml"]
|
||||
assert_includes styles_xml, 'w:styleId="Fancy"'
|
||||
assert_includes styles_xml, 'w:val="double"'
|
||||
assert_includes styles_xml, 'w:fill="F0F0F0"'
|
||||
assert_includes styles_xml, '<w:jc w:val="center"'
|
||||
assert_includes styles_xml, "<w:tblCellMar>"
|
||||
end
|
||||
|
||||
def test_per_edge_borders
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.define_table_style :mixed_borders,
|
||||
borders: {
|
||||
top: { style: "double", color: "FF0000", size: 8 },
|
||||
bottom: { style: "single", color: "000000", size: 4 },
|
||||
left: { style: "none" },
|
||||
right: { style: "none" },
|
||||
insideH: { style: "dotted", color: "CCCCCC", size: 2 },
|
||||
insideV: { style: "dotted", color: "CCCCCC", size: 2 }
|
||||
}
|
||||
doc.table(style: :mixed_borders) do
|
||||
doc.tr { doc.td "Cell" }
|
||||
end
|
||||
end
|
||||
|
||||
styles_xml = xml_files["word/styles.xml"]
|
||||
assert_includes styles_xml, '<w:top w:val="double"'
|
||||
assert_includes styles_xml, '<w:bottom w:val="single"'
|
||||
assert_includes styles_xml, '<w:left w:val="nil"'
|
||||
end
|
||||
|
||||
# --- Built-in styles tests ---
|
||||
|
||||
def test_built_in_grid_style_exists
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.table(style: :grid) do
|
||||
doc.tr { doc.td "Test" }
|
||||
end
|
||||
end
|
||||
|
||||
styles_xml = xml_files["word/styles.xml"]
|
||||
assert_includes styles_xml, 'w:styleId="Grid"'
|
||||
end
|
||||
|
||||
def test_built_in_borderless_style_exists
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.table(style: :borderless) do
|
||||
doc.tr { doc.td "Test" }
|
||||
end
|
||||
end
|
||||
|
||||
styles_xml = xml_files["word/styles.xml"]
|
||||
assert_includes styles_xml, 'w:styleId="Borderless"'
|
||||
end
|
||||
|
||||
def test_cell_margins_as_hash
|
||||
xml_files = create_doc_and_read_all_xml do |doc|
|
||||
doc.define_table_style :asymmetric_padding,
|
||||
cell_margins: { top: 100, bottom: 200, left: 150, right: 150 }
|
||||
doc.table(style: :asymmetric_padding) do
|
||||
doc.tr { doc.td "Cell" }
|
||||
end
|
||||
end
|
||||
|
||||
styles_xml = xml_files["word/styles.xml"]
|
||||
assert_includes styles_xml, "<w:tblCellMar>"
|
||||
assert_match(/<w:top[^>]*w:w="100"/, styles_xml)
|
||||
assert_match(/<w:bottom[^>]*w:w="200"/, styles_xml)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user