Skip to content

Latest commit

 

History

History
87 lines (83 loc) · 11.8 KB

IndentTextWriter.md

File metadata and controls

87 lines (83 loc) · 11.8 KB

class IndentTextWriter

A Text Writer, with a fluent API, for structured text generation incorporating:

  • Automatic Indentation management (using IndenSize and IndentChar).
  • Extensions for HTML writing with tag management.
  • Extensions for essential Markdown writing.
  • Text is output to an internal or external StringBuffer.
  • May be further extended to generate any kind of structured output.

Default Settings: IndentSize = 4 and IndentChar = space.

Members Description
Constructor:
C: IndentTextWriter() Create an IndentTextWriter using an internal StringBuffer to write Output to.
C: IndentTextWriter(StringBuilder output) Create an IndentTextWriter using an external StringBuffer to write Output to:
- Or creates an internal one of output is null.
Management:
P: int IndentSize Get or set the Indent Size (default = 4):
- May set to 0 (or a negative value) for no indentation.
- See also: SetIndentSize.
F: char IndentChar Get or set the Indent Char (default = space ' ' ):
- See also: SetIndentChar.
P: StringBuilder Output Get the attached or internal Output StringBuilder.
M: string AsString() Return Output as a String.
M: IndentTextWriter SaveAs(string fileName) Write Output to given file.
M: IndentTextWriter SetIndentSize(int indentSize) Set the Indent Size via a fluent service (default = 4):
- May set to 0 (or a negative value) for no indentation.
M: IndentTextWriter SetIndentChar(char indentChar) Set the Indent Char via a fluent service (default = space ' ' ).
Writing:
M: IndentTextWriter Indent(int indent = -1) Perform an Indent with the current (indent = -1) or once-off indent size.
- Adds a newline if not currently at a newline.

Parameters:
indent: Set to a value greater or equal to zero for a once-off override of the current IndentSize.
M: IndentTextWriter Outdent(int count = 1) Perform an Outdent for count number of times (count = -1 for all).
- Adds a newline if not currently at a newline.
M: IndentTextWriter Write(string text) Write text to Output with automated indentation if applicable.
M: IndentTextWriter WriteLine(string text = "") Write text and newline to output with automated indentation if applicable.
M: IndentTextWriter NewLine(int noof = 1) Write given number of newlines.
P: bool IsNewLine Query if last write operation ended with a new line.
M: IndentTextWriter Replace(string oldValue, string newValue) Replace all occurrences of oldValue with newValue in the current Output.

Parameters:
oldValue: Text to replace / remove.
newValue: Text to replace oldValue with (blank/null to just remove oldValue).
M: IndentTextWriter QuoteText(string text, char quoteChar = '"', bool escape = true) Write text wrapped within given quoteChar (default = ").

Parameters:
quoteChar: Quote character to use (default = ").
escape: Optionally escape the quoteChar if present in text (by inserting a \ before the character).
M: static string EscapeText(string escapeChars, string text) Escape all of the escapeChars in text (by inserting a \ before the character).
M: IndentTextWriter Wrap(string open, string close, Action<IndentTextWriter> content) Wrap content with the open and close strings:
- Writes: open + content + close
Block Writing:
M: IndentTextWriter WriteIndent(Action<IndentTextWriter> content) Write indented content:
- Writes: newline + indent + content + outdent + newline

Parameters:
content: Build the nested content
M: IndentTextWriter Block(string open, string close, Action<IndentTextWriter> content) Write indented content wrapped by the block open and close strings:
- Writes: open + newline + indent + content + outdent + newline + close + newline.
- If content is null writes: open + close + newline

Parameters:
open: Block opening text.
close: Block closing text.
content: Build the nested content
M: IndentTextWriter BlockCurly(Action<IndentTextWriter> content) Equivalent to: Block("{", "}", content)
M: IndentTextWriter BlockSquare(Action<IndentTextWriter> content) Equivalent to: Block("[", "]", content)

HTML Extensions

IndentTextWriter extensions for HTML writing and tag management:

  • Generally writes: <tag optional-attributes> Indented content... </tag>
  • Also methods for single line and self closing tags.
Extensions Description
HTML Writing:
E: HtmlTag(string tag, string attributes, Action<IndentTextWriter> content = null) Write indented content wrapped in the given HTML tag (with optional tag attributes):
- If content is null: then the empty tag will be written on a single line + newline.

Parameters:
tag: Tag to wrap the content in.
attributes: Optional attributes for the opening tag ("" or null for none).
content: Build the nested content.
E: HtmlTag(string tag, Action<IndentTextWriter> content = null) Write indented content wrapped in the given HTML tag (with no attributes)

Parameters:
tag: Tag to wrap the content in.
content: Build the nested content.
E: HtmlLineTag(string tag, string content, string attributes = null) Write content wrapped in the given HTML tag on a single (with optional attributes).

Parameters:
tag: Tag to wrap the content in.
content: Text content.
attributes: Optional attributes for the opening tag (default = none).
E: HtmlSelfClosingTag(string tag, string attributes) Write a self-closing HTML tag (no content) with optional attributes (e.g <hr>, <br>).

Parameters:
tag: Self closing tag
attributes: Optional attributes for the opening tag ("" or null for none)
E: HtmlAnchor(string anchorID) Write a HTML anchor element of the form: <a id="anchorID"></a>

Markdown Extensions

IdentTextWriter extensions for essential Markdown writing.

Extensions Description
Basic Markdown:
E: MdHR(this w) Markdown for a Horizontal Rule, and ensures a blank line before and after.
E: MdBr(this w) Create a HTML break tag: <br/>
E: MdLB(this w) Markdown for a Line Break by writing two spaces and a newline.
E: MdAnchor(string anchorID) Creates a HTML anchor element with given anchorID (for in-page navigation) : <a id="anchorID"></a>.
E: MdBold(string text) Wraps text in Bold markdown.
E: MdBold(Action<IndentTextWriter> content) Wraps content in Bold markdown.

Parameters:
content: Delegate to build the content.
E: MdItalic(string text) Wraps text in Italic markdown.
E: MdItalic(Action<IndentTextWriter> content) Wraps content in Italic markdown.

Parameters:
content: Delegate to build the content.
E: MdBoldItalic(string text) Wraps text in Bold and Italic markdown.
E: MdBoldItalic(Action<IndentTextWriter> content) Wraps content in Bold and Italic markdown.

Parameters:
content: Delegate to build the content.
E: MdCode(string text) Wraps text in Code markdown with single back tick.
E: MdCode(Action<IndentTextWriter> content) Wraps content in Code markdown with single back tick.

Parameters:
content: Delegate to build the content.
E: MdCode2(string text) Wraps text in Code markdown with double back ticks (use if text contains any back ticks).
E: MdCode2(Action<IndentTextWriter> content) Wraps content in Code markdown with double back ticks (use if content contains any back ticks).

Parameters:
content: Delegate to build the content.
E: MdLink(string title, string url) Markdown for a link.

Parameters:
title: Link title.
url: Link Url.
E: MdImage(string altText, string imagePathOrUrl, string title = null) Markdown for an Image.

Parameters:
altText: Image alt-text (i.e. if image can't be displayed).
imagePathOrUrl: Path or Url to image.
title: Optional image tool-tip title.
E: MdLinkImage(string altText, string imagePathOrUrl, string imageLinkUrl, string title = null) Markdown for a Image and Link.

Parameters:
altText: Image alt-text (i.e. if image can't be displayed).
imagePathOrUrl: Path or Url to image.
imageLinkUrl: Url for the image link.
title: Optional image tool-tip title.
Table building:
E: MdTableHeader(string align, params string[] headers) Markdown for a table header.

Parameters:
align: String of alignment characters for each column: L = left, C = Center, R = Right.
headers: Header for each column (as a variable number or arguments).
E: MdTableCol(string text, bool first = false) Markdown for a column with given text (writes: "| text |" if first else "space text |")
- Add a newline after the last column to complete a row.

Parameters:
first: Set to true if this is the first column.
E: MdTableCol(Action<IndentTextWriter> content, bool first = false) Markdown for a column with given content (writes: "| content |" if first else "space content |")
- Add a newline after the last column to complete a row.

Parameters:
content: Delegate to build the content.
first: Set to true if this is the first column.
E: MdTableRow(params Action<IndentTextWriter>[] cols) Markdown for a complete table row with a delegate to build the content for each column.

Parameters:
cols: Delegate to build the content for each column (as a variable number or arguments).
E: MdTableRow(params string[] cols) Markdown for a complete table row with given text for each column.

Parameters:
cols: Text for each column (as a variable number or arguments).
Badges: Badge Styles: flat (default), flat-square, plastic, social, for-the-badge
E: MdBadge(string subject, string status, string color = "blue", string style = "flat") Create a generic badge with: subject, status and color:
- E.g. "Tests", "Passing", "green"

Parameters:
subject: Badge subject.
status: Subject status.
color: Color name.
E: MdBadgeNugetV(string packageName, string style = "flat") Create a Nuget Version badge (auto retrieves the version from Nuget).

Parameters:
packageName: Full name of package on Nuget.
E: MdBadgeNugetDt(string packageName, string style = "flat") Create a Nuget Downloads badge (auto retrieves the downloads from Nuget).

Parameters:
packageName: Full name of package on Nuget.
E: MdBadgeCSharp(string style = "flat") Create a C# badge
E: MdBadgeLicenseMIT(string style = "flat") Create a MIT license badge.