Grantlee 5.3.0

Public Member Functions | List of all members
Grantlee::TextHTMLBuilder Class Reference

The TextHTMLBuilder creates a clean html markup output. More...

`#include <grantlee/texthtmlbuilder.h>`

Inheritance diagram for Grantlee::TextHTMLBuilder:
[legend]

## Public Member Functions
void | addNewline () override
void | appendLiteralText (const QString &text) override
void | appendRawText (const QString &text) override
void | beginAnchor (const QString &href={}, const QString &name={}) override
void | beginBackground (const QBrush &brush) override
void | beginEmph () override
void | beginFontFamily (const QString &family) override
void | beginFontPointSize (int size) override
void | beginForeground (const QBrush &brush) override
void | beginHeader (int level) override
void | beginList (QTextListFormat::Style type) override
void | beginListItem () override
void | beginParagraph (Qt::Alignment al=Qt::AlignLeft, qreal topMargin=0.0, qreal bottomMargin=0.0, qreal leftMargin=0.0, qreal rightMargin=0.0) override
void | beginStrikeout () override
void | beginStrong () override
void | beginSubscript () override
void | beginSuperscript () override
void | beginTable (qreal cellpadding, qreal cellspacing, const QString &width) override
void | beginTableCell (const QString &width, int colspan, int rowspan) override
void | beginTableHeaderCell (const QString &width, int colspan, int rowspan) override
void | beginTableRow () override
void | beginUnderline () override
void | endAnchor () override
void | endBackground () override
void | endEmph () override
void | endFontFamily () override
void | endFontPointSize () override
void | endForeground () override
void | endHeader (int level) override
void | endList () override
void | endListItem () override
void | endParagraph () override
void | endStrikeout () override
void | endStrong () override
void | endSubscript () override
void | endSuperscript () override
void | endTable () override
void | endTableCell () override
void | endTableHeaderCell () override
void | endTableRow () override
void | endUnderline () override
QString | getResult () override
void | insertHorizontalRule (int width=-1) override
void | insertImage (const QString &src, qreal width, qreal height) override
Public Member Functions inherited from Grantlee::AbstractMarkupBuilder
virtual | ~AbstractMarkupBuilder ()
virtual void | addNewline ()=0
virtual void | appendLiteralText (const QString &text)=0
virtual void | appendRawText (const QString &text)=0
virtual void | beginAnchor (const QString &href={}, const QString &name={})=0
virtual void | beginBackground (const QBrush &brush)=0
virtual void | beginEmph ()=0
virtual void | beginFontFamily (const QString &family)=0
virtual void | beginFontPointSize (int size)=0
virtual void | beginForeground (const QBrush &brush)=0
virtual void | beginHeader (int level)=0
virtual void | beginList (QTextListFormat::Style style)=0
virtual void | beginListItem ()=0
virtual void | beginParagraph (Qt::Alignment a=Qt::AlignLeft, qreal top=0.0, qreal bottom=0.0, qreal left=0.0, qreal right=0.0)=0
virtual void | beginStrikeout ()=0
virtual void | beginStrong ()=0
virtual void | beginSubscript ()=0
virtual void | beginSuperscript ()=0
virtual void | beginTable (qreal cellpadding, qreal cellspacing, const QString &width)=0
virtual void | beginTableCell (const QString &width, int colSpan, int rowSpan)=0
virtual void | beginTableHeaderCell (const QString &width, int colSpan, int rowSpan)=0
virtual void | beginTableRow ()=0
virtual void | beginUnderline ()=0
virtual void | endAnchor ()=0
virtual void | endBackground ()=0
virtual void | endEmph ()=0
virtual void | endFontFamily ()=0
virtual void | endFontPointSize ()=0
virtual void | endForeground ()=0
virtual void | endHeader (int level)=0
virtual void | endList ()=0
virtual void | endListItem ()=0
virtual void | endParagraph ()=0
virtual void | endStrikeout ()=0
virtual void | endStrong ()=0
virtual void | endSubscript ()=0
virtual void | endSuperscript ()=0
virtual void | endTable ()=0
virtual void | endTableCell ()=0
virtual void | endTableHeaderCell ()=0
virtual void | endTableRow ()=0
virtual void | endUnderline ()=0
virtual QString | getResult ()=0
virtual void | insertHorizontalRule (int width=-1)=0
virtual void | insertImage (const QString &url, qreal width, qreal height)=0

## Detailed Description

This class creates html output which is as minimal as possible and restricted to the rich text features supported in Qt. (https://doc.qt.io/qt-5/richtext-html-subset.html)

The output contains only the body content, not the head element or other metadata.

eg:

<p>
This is some <strong>formatted content</strong> in a paragraph.
</p>

instead of the content produced by Qt:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<html><head><meta name="qrichtext" content="1" /><meta
http-equiv="Content-Type" content="text/html; charset=UTF-8" /><style
type="text/css">
p, li { white-space: pre-wrap; }
</style></head><body style=" font-family:'Sans Serif'; font-size:10pt;
font-weight:400; font-style:normal;">
<p style=" margin-top:12px; margin-bottom:12px; margin-left:0px;
margin-right:0px; -qt-block-indent:0; text-indent:0px;">This is some <span
style=" font-weight:600;">formatted content</span> in a paragraph.
</p></body></html>

Such tags should be created separately. For example:

auto b = new TextHTMLBuilder();
auto md = new MarkupDirector(b);
md->constructContent();
QString cleanHtml(
"<head>\n<title>%1</title>\n</head>\n<body>%2</body>\n</html>")
.arg(document.metaInformation(QTextDocument::DocumentTitle))
.arg(b->getOutput());
file.write(cleanHtml);
Grantlee::MarkupDirector
Instructs a builder object to create markup output.
Definition: markupdirector.h:93
Grantlee::TextHTMLBuilder
The TextHTMLBuilder creates a clean html markup output.
Definition: texthtmlbuilder.h:113

Font formatting information on elements is represented by individual span elements.

eg:

<span style"color:blue;">
<span style="background-color:red;">
Blue text on red background
</span>
</span>

instead of

<span style="color:blue;background-color:red;">
Blue text on red background
</span>

It my be possible to change this if necessary.

Author
  Stephen Kelly steve.nosp@m.ire@.nosp@m.gmail.nosp@m..com

Definition at line 111 of file texthtmlbuilder.h.

## Member Function Documentation

## ◆ addNewline()

void Grantlee::TextHTMLBuilder::addNewline | ( |  | ) |
| override virtual

Add a newline to the markup.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ appendLiteralText()

void Grantlee::TextHTMLBuilder::appendLiteralText | ( | const QString & | text | ) |
| override virtual

Reimplemented from AbstractMarkupBuilder.

This implementation escapes the text before appending so that

      A sample <b>bold</b> word.

becomes

      A sample &lt;b&gt;bold&lt;/b&gt; word.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ appendRawText()

void Grantlee::TextHTMLBuilder::appendRawText | ( | const QString & | text | ) |
| override virtual

Append `text` without escaping.

This is useful if extending MarkupDirector

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginAnchor()

void Grantlee::TextHTMLBuilder::beginAnchor | ( | const QString & | href = `{}`,
|  | const QString & | name = `{}`
| ) |  |
| override virtual

Begin a url anchor element in the markup

Parameters

  href | The href of the anchor.
  name | The name of the anchor.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginBackground()

void Grantlee::TextHTMLBuilder::beginBackground | ( | const QBrush & | brush | ) |
| override virtual

Begin a decorarated background element in the markup (A text background color) using `brush`

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginEmph()

void Grantlee::TextHTMLBuilder::beginEmph | ( |  | ) |
| override virtual

Begin an emphasised element in the markup

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginFontFamily()

void Grantlee::TextHTMLBuilder::beginFontFamily | ( | const QString & | family | ) |
| override virtual

Begin a new font familiy element in the markup

Parameters

  family | The name of the font family to begin.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginFontPointSize()

void Grantlee::TextHTMLBuilder::beginFontPointSize | ( | int | size | ) |
| override virtual

Begin a new font point size

Parameters

  size | The new size to begin.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginForeground()

void Grantlee::TextHTMLBuilder::beginForeground | ( | const QBrush & | brush | ) |
| override virtual

Begin a decorarated foreground element in the markup (A text color) using `brush`

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginHeader()

void Grantlee::TextHTMLBuilder::beginHeader | ( | int | level | ) |
| override virtual

Begin a new header element.

Parameters

  level | The new level to begin.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginList()

void Grantlee::TextHTMLBuilder::beginList | ( | QTextListFormat::Style | style | ) |
| override virtual

Begin a new list element in the markup. A list element contains list items, and may contain other lists.

Parameters

  style | The style of list to create.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginListItem()

void Grantlee::TextHTMLBuilder::beginListItem | ( |  | ) |
| override virtual

Begin a new list item in the markup

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginParagraph()

void Grantlee::TextHTMLBuilder::beginParagraph | ( | Qt::Alignment | al = `Qt::AlignLeft`,
|  | qreal | topMargin = `0.0`,
|  | qreal | bottomMargin = `0.0`,
|  | qreal | leftMargin = `0.0`,
|  | qreal | rightMargin = `0.0`
| ) |  |
| override virtual

Begin a new paragraph

Parameters

  al | The new paragraph alignment
  topMargin | The new paragraph topMargin
  bottomMargin | The new paragraph bottomMargin
  leftMargin | The new paragraph leftMargin
  rightMargin | The new paragraph rightMargin

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginStrikeout()

void Grantlee::TextHTMLBuilder::beginStrikeout | ( |  | ) |
| override virtual

Begin a struck out element in the markup

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginStrong()

void Grantlee::TextHTMLBuilder::beginStrong | ( |  | ) |
| override virtual

Begin a bold element in the markup

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginSubscript()

void Grantlee::TextHTMLBuilder::beginSubscript | ( |  | ) |
| override virtual

Begin a subscript element

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginSuperscript()

void Grantlee::TextHTMLBuilder::beginSuperscript | ( |  | ) |
| override virtual

Begin a superscript element

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginTable()

void Grantlee::TextHTMLBuilder::beginTable | ( | qreal | cellpadding,
|  | qreal | cellspacing,
|  | const QString & | width
| ) |  |
| override virtual

Begin a table element.

Parameters

  cellpadding | The padding attribute for the table.
  cellspacing | The spacing attribute for the table.
  width | The width of the table. May be either an integer, or a percentage value.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginTableCell()

void Grantlee::TextHTMLBuilder::beginTableCell | ( | const QString & | width,
|  | int | colSpan,
|  | int | rowSpan
| ) |  |
| override virtual

Begin a new table cell.

Parameters

  width | The width of the cell.
  colSpan | The column span of the cell.
  rowSpan | The row span of the cell.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginTableHeaderCell()

void Grantlee::TextHTMLBuilder::beginTableHeaderCell | ( | const QString & | width,
|  | int | colSpan,
|  | int | rowSpan
| ) |  |
| override virtual

Begin a new table header cell.

Parameters

  width | The width of the cell.
  colSpan | The column span of the cell.
  rowSpan | The row span of the cell.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginTableRow()

void Grantlee::TextHTMLBuilder::beginTableRow | ( |  | ) |
| override virtual

Begin a new table row

Implements Grantlee::AbstractMarkupBuilder.

## ◆ beginUnderline()

void Grantlee::TextHTMLBuilder::beginUnderline | ( |  | ) |
| override virtual

Begin an underlined element in the markup

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endAnchor()

void Grantlee::TextHTMLBuilder::endAnchor | ( |  | ) |
| override virtual

Close the anchor element

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endBackground()

void Grantlee::TextHTMLBuilder::endBackground | ( |  | ) |
| override virtual

Close the decorarated background element in the markup

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endEmph()

void Grantlee::TextHTMLBuilder::endEmph | ( |  | ) |
| override virtual

Close the emphasised element in the markup

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endFontFamily()

void Grantlee::TextHTMLBuilder::endFontFamily | ( |  | ) |
| override virtual

End font family element

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endFontPointSize()

void Grantlee::TextHTMLBuilder::endFontPointSize | ( |  | ) |
| override virtual

End font point size element

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endForeground()

void Grantlee::TextHTMLBuilder::endForeground | ( |  | ) |
| override virtual

Close the decorarated foreground element in the markup

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endHeader()

void Grantlee::TextHTMLBuilder::endHeader | ( | int | level | ) |
| override virtual

End a header element.

Parameters

  level | The new level to end.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endList()

void Grantlee::TextHTMLBuilder::endList | ( |  | ) |
| override virtual

Close the list.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endListItem()

void Grantlee::TextHTMLBuilder::endListItem | ( |  | ) |
| override virtual

End the list item

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endParagraph()

void Grantlee::TextHTMLBuilder::endParagraph | ( |  | ) |
| override virtual

Close the paragraph in the markup.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endStrikeout()

void Grantlee::TextHTMLBuilder::endStrikeout | ( |  | ) |
| override virtual

Close the struck out element in the markup

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endStrong()

void Grantlee::TextHTMLBuilder::endStrong | ( |  | ) |
| override virtual

Close the bold element in the markup

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endSubscript()

void Grantlee::TextHTMLBuilder::endSubscript | ( |  | ) |
| override virtual

End subscript element

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endSuperscript()

void Grantlee::TextHTMLBuilder::endSuperscript | ( |  | ) |
| override virtual

End superscript element

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endTable()

void Grantlee::TextHTMLBuilder::endTable | ( |  | ) |
| override virtual

End a table element

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endTableCell()

void Grantlee::TextHTMLBuilder::endTableCell | ( |  | ) |
| override virtual

End a table cell

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endTableHeaderCell()

void Grantlee::TextHTMLBuilder::endTableHeaderCell | ( |  | ) |
| override virtual

End a table header cell

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endTableRow()

void Grantlee::TextHTMLBuilder::endTableRow | ( |  | ) |
| override virtual

End a table row

Implements Grantlee::AbstractMarkupBuilder.

## ◆ endUnderline()

void Grantlee::TextHTMLBuilder::endUnderline | ( |  | ) |
| override virtual

Close the underlined element in the markup

Implements Grantlee::AbstractMarkupBuilder.

## ◆ getResult()

QString Grantlee::TextHTMLBuilder::getResult | ( |  | ) |
| override virtual

Return the fully marked up result of the building process.

This may contain metadata etc, such as a head element in html.

Returns
  The fully marked up text.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ insertHorizontalRule()

void Grantlee::TextHTMLBuilder::insertHorizontalRule | ( | int | width = `-1` | ) |
| override virtual

Insert a horizontal rule into the markup.

Parameters

  width | The width of the rule. Default is full width.

Implements Grantlee::AbstractMarkupBuilder.

## ◆ insertImage()

void Grantlee::TextHTMLBuilder::insertImage | ( | const QString & | url,
|  | qreal | width,
|  | qreal | height
| ) |  |
| override virtual

Insert a new image element into the markup.

Parameters

  url | The url of the image
  width | The width of the image
  height | The height of the image.

Implements Grantlee::AbstractMarkupBuilder.