Rich Text Elements

RichTextElements

Description

(simple but) rich text formatting tools

julia> import BibTeXFormat: RichText, Tag, render_as

julia> import BibTeXFormat.RichTextElements: add_period, capitalize

julia> t = RichText("this ", "is a ", Tag("em", "very"), RichText(" rich", " text"));

julia> render_as(t,"LaTex")
"this is a \\emph{very} rich text"

julia> convert(String,t)
"this is a very rich text"

julia> t = add_period(capitalize(t));

julia> render_as(t,"latex")
"This is a \\emph{very} rich text."

Types

Functions

Reference

function capitalize(self::BaseText)

Capitalize the first letter of the text and lowercasecase the rest.

julia> import BibTeXFormat.RichTextElements: capitalize, RichText,  Tag

julia> capitalize(RichText(Tag("em", "LONG CAT")))
RichText(Tag("em", "Long cat"))
source

A RichString is a wrapper for a plain Julia string.

julia> import BibTeXFormat.RichTextElements: RichString, render_as

julia> print(render_as(RichString("Crime & Punishment"),"text"))
Crime & Punishment

julia> print(render_as(RichString("Crime & Punishment"),"html"))
Crime & Punishment
source

All arguments must be plain unicode strings. Arguments are concatenated together.

julia> import BibTeXFormat.RichTextElements: RichString

julia> print(convert(String,RichString("November", ", ", "December", ".")))
November, December.
source
Base.:+Method.
function +(b::BaseText, other)

Concatenate this Text with another Text or string.

julia> import BibTeXFormat.RichTextElements: RichText, Tag, append, render_as

julia> a = RichText("Longcat is ") + Tag("em", "long")
RichText("Longcat is ", Tag("em", "long"))
source
Base.:==Method.

Compare two RichString objects.

source

Return True if all characters in the string are alphabetic and there is at least one character, False otherwise.

source
function isletter(self::TextSymbol)

A TextSymbol is not alfanumeric. Returns false

source
function isletter(self::T) where T<:MultiPartText

Return true if all characters in the string are alphabetic and there is at least one character, False otherwise.

source
function lowercase(self::T) where T <:MultiPartText

Convert rich text to lowercasecase.

julia> import BibTeXFormat.RichTextElements: RichText, Tag

julia> lowercase(RichText(Tag("em", "Long cat")))
RichText(Tag("em", "long cat"))
source

Convert rich text to uppsercase.

julia> import BibTeXFormat.RichTextElements: RichText, Tag

julia> uppercase(RichText(Tag("em", "Long cat")))
RichText(Tag("em", "LONG CAT"))
source
Base.endswithMethod.
function endswith(self::RichString, suffix)

Return True if the string ends with the specified suffix, otherwise return False.

suffix can also be a tuple of suffixes to look for. return value.endswith(self.value text)

source
Base.endswithMethod.

Return True if the text ends with the given suffix.

julia> import BibTeXFormat.RichTextElements: RichText

julia> endswith(RichText("Longcat!"),"cat!")
true

Suffixes split across multiple parts are not matched:

julia> import BibTeXFormat.RichTextElements: RichText, Tag

julia> endswith(RichText("Long", Tag("em", "cat"), "!"),"cat!")
false
source
Base.getindexMethod.

Slicing and extracting characters works like with regular strings, formatting is preserved.

julia> import BibTeXFormat.RichTextElements: RichText, Tag

julia> RichText("Longcat is ", Tag("em", "looooooong!"))[1:15]
RichText("Longcat is ", Tag("em", "looo"))

julia> RichText("Longcat is ", Tag("em", "looooooong!"))[end]
RichText(Tag("em", "!"))
source
Base.joinMethod.
function join(self::T, parts) where T<:BaseText

Join a list using this text (like join)

julia> import BibTeXFormat.RichTextElements: RichString

julia> letters = ["a", "b", "c"];

julia> print(convert(String,join(RichString("-"),letters)))
a-b-c
source
Base.lengthMethod.

$lenght(text)$ returns the number of characters in the text, ignoring the markup:

julia> import BibTeXFormat.RichTextElements: Tag, RichText, HRef

julia> length(RichText("Long cat"))
8
julia> length(RichText(Tag("em", "Long"), " cat"))
8
julia> length(RichText(HRef("http://example.com/", "Long"), " cat"))
8
source
Base.occursinMethod.

$value in text$ returns $True$ if any part of the $text$ occursin the substring $value$:

julia> import BibTeXFormat.RichTextElements: RichText, occursin

julia> occursin("Long cat", RichText("Long cat!"))
true

Substrings splitted across multiple text parts are not matched:

julia> import BibTeXFormat.RichTextElements: RichText, occursin, Tag

julia> occursin("Long cat", RichText(Tag("em", "Long"), "cat!"))
false
source
Base.splitFunction.
function split(self::RichString, sep=nothing; keep_empty_parts=nothing)

Split

source
Base.splitMethod.
function split(self::T, sep=nothing; keep_empty_parts=nothing) where T <:MultiPartText
julia> import BibTeXFormat.RichTextElements: RichText, split

julia> print(split(RichText("a + b")))
Any[RichText("a"), RichText("+"), RichText("b")]

julia> print(split(RichText("a, b"), ", "))
Any[RichText("a"), RichText("b")]
source
Base.startswithMethod.
function startswith(self::RichString, prefix)

Return True if string starts with the prefix, otherwise return False.

prefix can also be a tuple of suffixes to look for.

source
Base.startswithMethod.
function startswith(self::T, prefix) where T<:MultiPartText

Return True if the text starts with the given prefix.

julia> import BibTeXFormat.RichTextElements: RichText

julia> startswith(RichText("Longcat!"),"Longcat")
true

Prefixes split across multiple parts are not matched:

julia> import BibTeXFormat.RichTextElements: RichText, Tag

julia> startswith(RichText(Tag("em", "Long"), "cat!"),"Longcat")
false
source
function add_period(self::BaseText, period=".")

Add a period to the end of text, if the last character is not ".", "!" or "?".

julia> import BibTeXFormat.RichTextElements: RichText, add_period

julia> text = RichText("That's all, folks");

julia> print(convert(String,add_period(text)))
That's all, folks.

julia> text = RichText("That's all, folks!");

julia> print(convert(String,add_period(text)))
That's all, folks!
source
function append(self::BaseText, text)

Append text to the end of this text.

Normally, this is the same as concatenating texts with +, but for tags and similar objects the appended text is placed inside the tag.

julia> import BibTeXFormat.RichTextElements: RichText, Tag, append, render_as

julia> text = Tag("em", "Look here");

julia> print(render_as(text + "!","html"))
<em>Look here</em>!

julia> print(render_as(append(text,"!"),"html"))
<em>Look here!</em>
source
function  append(self::T, text) where T<:MultiPartText

Append text to the end of this text.

For Tags, HRefs, etc. the appended text is placed inside the tag.

julia> import BibTeXFormat.RichTextElements: Tag, render_as, HRef, append

julia> text = Tag("strong", "Chuck Norris");

julia> print(render_as(text +  " wins!","html"))
<strong>Chuck Norris</strong> wins!

julia> print(render_as(append(text," wins!"),"html"))
<strong>Chuck Norris wins!</strong>
source
function capfirst(self::BaseText)

Capitalize the first letter of the text.

julia> import BibTeXFormat.RichTextElements: capfirst, RichText,  Tag

julia> capfirst(RichText(Tag("em", "long Cat")))
RichText(Tag("em", "Long Cat"))
source
function create_similar(self::T, parts) where T<:MultiPartText

Create a new text object of the same type with the same parameters, with different text content.

julia> import BibTeXFormat.RichTextElements: Tag, create_similar

julia> text = Tag("strong", "Bananas!");

julia> create_similar(text,["Apples!"])
Tag("strong", "Apples!")
source

Initialize the parts. A MutliPartText elements must have the following members

type atype <: MultiPartText
	parts
	length
	info
end

Empty parts are ignored:

julia> import BibTeXFormat.RichTextElements: Tag, RichText, HRef

julia> RichText() == RichText("") == RichText("", "", "")
true
julia> RichText("Word", "") == RichText("Word")
true

Text() objects are unpacked and their children are included directly:

julia> import BibTeXFormat.RichTextElements: Tag, RichText, HRef

julia> RichText(RichText("Multi", " "), Tag("em", "part"), RichText(" ", RichText("text!")))
RichText("Multi ", Tag("em", "part"), " text!")

julia> Tag("strong", RichText("Multi", " "), Tag("em", "part"), RichText(" ", "text!"))
Tag("strong", "Multi ", Tag("em", "part"), " text!")

Similar objects are merged together:

julia> import BibTeXFormat.RichTextElements: Tag, RichText, HRef

julia> RichText("Multi", Tag("em", "part"), RichText(Tag("em", " ", "text!")))
RichText("Multi", Tag("em", "part text!"))

julia> RichText("Please ", HRef("/", "click"), HRef("/", " here"), ".")
RichText("Please ", HRef("/", "click here"), ".")
source
function merge_similar(param_parts)

Merge adjacent text objects with the same type and parameters together.

julia> import BibTeXFormat.RichTextElements: RichText, Tag, merge_similar

julia> parts = [Tag("em", "Breaking"), Tag("em", " "), Tag("em", "news!")];

julia> print(merge_similar(parts))
Any[Tag("em", "Breaking news!")]

julia> parts = [Tag("em", "Breaking"), Tag("em", " "), Tag("ol", "news!")];

julia> print(merge_similar(parts))
Any[Tag("em", "Breaking "), Tag("ol", "news!")]
source

Return a text consistng of the first slice_length characters of this text (with formatting preserved).

source
function slice_end(self::T, slice_length::Integer) where T<:MultiPartText

Return a text consistng of the last slice_length characters of this text (with formatting preserved).

source
function  typeinfo(self::T) where T<:MultiPartText

Return the type and the parameters used to create this text object.

julia> using BibTeXFormat

julia> import BibTeXFormat.RichTextElements: Tag, typeinfo

julia> text = Tag("strong", "Heavy rain!");

julia> typeinfo(text) == ("Tag", Tag, "strong")
true
source