Files
Notare/examples/full_demo.rb
mathias234 9a70d91fd5
All checks were successful
CI Pipeline / build (pull_request) Successful in 14s
Implement table styles
2025-12-03 12:14:31 +01:00

413 lines
12 KiB
Ruby

# frozen_string_literal: true
# Full demo of all Notare features
# Run with: bundle exec ruby examples/full_demo.rb
require_relative "../lib/notare"
OUTPUT_FILE = File.expand_path("../example.docx", __dir__)
FIXTURES_DIR = File.expand_path("../test/fixtures", __dir__)
Notare::Document.create(OUTPUT_FILE) do |doc|
# ============================================================================
# Custom Styles
# ============================================================================
doc.define_style :highlight, bold: true, color: "FF6600"
doc.define_style :success, color: "228B22", italic: true
doc.define_style :centered_large, align: :center, size: 16, bold: true
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
# ============================================================================
doc.h1 "Notare Feature Demo"
doc.p "A comprehensive example of all supported features", style: :subtitle
# ============================================================================
# 1. Text Formatting
# ============================================================================
doc.h2 "1. Text Formatting"
doc.p do
doc.text "This paragraph demonstrates "
doc.b { doc.text "bold" }
doc.text ", "
doc.i { doc.text "italic" }
doc.text ", "
doc.u { doc.text "underlined" }
doc.text ", "
doc.s { doc.text "strikethrough" }
doc.text ", and "
doc.b do
doc.i do
doc.u { doc.text "combined" }
end
end
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
# ============================================================================
doc.h2 "2. Headings"
doc.h3 "This is Heading 3"
doc.h4 "This is Heading 4"
doc.h5 "This is Heading 5"
doc.h6 "This is Heading 6"
# ============================================================================
# 3. Built-in Styles
# ============================================================================
doc.h2 "3. Built-in Styles"
doc.p "This is styled as a title", style: :title
doc.p "This is styled as a subtitle", style: :subtitle
doc.p "This is styled as a quote - perfect for citations and quotations.", style: :quote
doc.p "def hello; puts \"world\"; end", style: :code
# ============================================================================
# 4. Custom Styles
# ============================================================================
doc.h2 "4. Custom Styles"
doc.p "This text uses our custom highlight style!", style: :highlight
doc.p do
doc.text "Mixed styles: "
doc.text "success message", style: :success
doc.text " and "
doc.text "highlighted text", style: :highlight
doc.text " in one paragraph."
end
doc.p "Centered and large text", style: :centered_large
doc.p "This was removed from the document", style: :deleted_text
doc.p "This is critically important!", style: :important
# ============================================================================
# 5. Text Highlighting
# ============================================================================
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
doc.li "First item"
doc.li "Second item"
doc.li do
doc.text "Item with "
doc.b { doc.text "bold" }
doc.text " text"
end
end
doc.h3 "Numbered List"
doc.ol do
doc.li "Step one"
doc.li "Step two"
doc.li "Step three"
end
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
# ============================================================================
# 8. Hyperlinks
# ============================================================================
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" } }
doc.td { doc.b { doc.text "Status" } }
doc.td { doc.b { doc.text "Notes" } }
end
doc.tr do
doc.td "Paragraphs"
doc.td { doc.text "Complete", style: :success }
doc.td "Basic text support"
end
doc.tr do
doc.td "Formatting"
doc.td { doc.text "Complete", style: :success }
doc.td "Bold, italic, underline, 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"
doc.td { doc.text "Complete", style: :success }
doc.td "h1 through h6"
end
doc.tr do
doc.td "Styles"
doc.td { doc.text "Complete", style: :success }
doc.td "Built-in and custom"
end
doc.tr do
doc.td "Images"
doc.td { doc.text "Complete", style: :success }
doc.td "PNG and JPEG"
end
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
# ============================================================================
# 10. Images
# ============================================================================
doc.h2 "10. Images"
doc.p "Image with explicit dimensions:"
doc.p do
doc.image File.join(FIXTURES_DIR, "test.png"), width: "2in", height: "2in"
end
doc.p "Inline image with text:"
doc.p do
doc.text "Before "
doc.image File.join(FIXTURES_DIR, "test.jpg"), width: "0.75in", height: "0.75in"
doc.text " After"
end
doc.p "Image in a table:"
doc.table do
doc.tr do
doc.td "Description"
doc.td do
doc.image File.join(FIXTURES_DIR, "test.png"), width: "1in", height: "1in"
end
end
end
# ============================================================================
# 11. Page Breaks
# ============================================================================
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" }
doc.text " "
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
puts "Created #{OUTPUT_FILE}"