public class Drafty
extends java.lang.Object
implements java.io.Serializable
Basic parser and formatter for very simple rich text. Mostly targeted at mobile use cases similar to Telegram and WhatsApp.
Supports:
Nested formatting is supported, e.g. *abc _def_* → abc def
URLs, @mentions, and #hashtags are extracted.
JSON data representation is similar to Draft.js raw formatting.
Sample text:
this is *bold*, `code` and _italic_, ~strike~ combined *bold and _italic_* an url: https://www.example.com/abc#fragment and another _www.tinode.co_ this is a @mention and a #hashtag in a string second #hashtag
JSON representation of the sample text above:
{ "txt": "this is bold, code and italic, strike combined bold and italic an url: https://www.example.com/abc#fragment " + "and another www.tinode.co this is a @mention and a #hashtag in a string second #hashtag", "fmt": [ { "at":8, "len":4,"tp":"ST" },{ "at":14, "len":4, "tp":"CO" },{ "at":23, "len":6, "tp":"EM"}, { "at":31, "len":6, "tp":"DL" },{ "tp":"BR", "len":1, "at":37 },{ "at":56, "len":6, "tp":"EM" }, { "at":47, "len":15, "tp":"ST" },{ "tp":"BR", "len":1, "at":62 },{ "at":120, "len":13, "tp":"EM" }, { "at":71, "len":36, "key":0 },{ "at":120, "len":13, "key":1 },{ "tp":"BR", "len":1, "at":133 }, { "at":144, "len":8, "key":2 },{ "at":159, "len":8, "key":3 },{ "tp":"BR", "len":1, "at":179 }, { "at":187, "len":8, "key":3 },{ "tp":"BR", "len":1, "at":195 } ], "ent": [ { "tp":"LN", "data":{ "url":"https://www.example.com/abc#fragment" } }, { "tp":"LN", "data":{ "url":"http://www.tinode.co" } }, { "tp":"MN", "data":{ "val":"mention" } }, { "tp":"HT", "data":{ "val":"hashtag" } } ] }
Modifier and Type | Class and Description |
---|---|
static class |
Drafty.Entity |
static interface |
Drafty.Formatter<T> |
static class |
Drafty.Node |
static class |
Drafty.Style |
static interface |
Drafty.Transformer |
Modifier and Type | Field and Description |
---|---|
Drafty.Entity[] |
ent |
Drafty.Style[] |
fmt |
static java.lang.String |
MIME_TYPE |
java.lang.String |
txt |
Modifier | Constructor and Description |
---|---|
|
Drafty() |
|
Drafty(java.lang.String content) |
protected |
Drafty(java.lang.String text,
Drafty.Style[] fmt,
Drafty.Entity[] ent) |
Modifier and Type | Method and Description |
---|---|
Drafty |
append(@Nullable Drafty that)
Append one Drafty document to another Drafty document
|
Drafty |
appendLineBreak()
Append line break 'BR' to Drafty document.
|
Drafty |
attachFile(java.lang.String mime,
byte[] bits,
java.lang.String fname)
Attach file to a drafty object in-band.
|
protected Drafty |
attachFile(java.lang.String mime,
byte[] bits,
java.lang.String fname,
java.lang.String refurl,
long size)
Attach file to a drafty object.
|
Drafty |
attachFile(java.lang.String mime,
java.lang.String fname,
java.lang.String refurl,
long size)
Attach file to a drafty object as a reference.
|
Drafty |
attachJSON(@NotNull java.util.Map<java.lang.String,java.lang.Object> json)
Attach object as json.
|
protected static void |
attachmentsToEnd(Drafty.Node tree,
int maxAttachments) |
boolean |
equals(java.lang.Object another) |
<T> T |
format(@NotNull Drafty.Formatter<T> formatter)
Format converts Drafty object into a collection of formatted nodes.
|
@Nullable Drafty |
forwardedContent()
Remove leading @mention from Drafty document and any leading line breaks making document
suitable for forwarding.
|
static Drafty |
fromPlainText(java.lang.String plainText)
Creates Drafty document with txt set to the parameter without any parsing.
|
Drafty.Entity[] |
getEntities() |
java.lang.String[] |
getEntReferences()
Extract attachment references for use in message header.
|
boolean |
hasEntities(java.lang.Iterable<java.lang.String> types) |
Drafty |
insert(int at,
@Nullable java.lang.String text,
@Nullable java.lang.String style,
@Nullable java.util.Map<java.lang.String,java.lang.Object> data)
Insert a styled text at the given location.
|
Drafty |
insertAudio(int at,
@NotNull java.lang.String mime,
byte[] bits,
byte[] preview,
int duration,
@Nullable java.lang.String fname,
@Nullable java.net.URI refurl,
long size)
Insert audio recording into Drafty document.
|
protected Drafty |
insertButton(int at,
@Nullable java.lang.String title,
@Nullable java.lang.String id,
@NotNull java.lang.String actionType,
@Nullable java.lang.String actionValue,
@Nullable java.lang.String refUrl)
Insert button into Drafty document.
|
Drafty |
insertImage(int at,
java.lang.String mime,
byte[] bits,
int width,
int height,
java.lang.String fname)
Insert inline image
|
Drafty |
insertImage(int at,
@Nullable java.lang.String mime,
byte[] bits,
int width,
int height,
@Nullable java.lang.String fname,
@Nullable java.net.URI refurl,
long size)
Insert inline image
|
boolean |
isPlain()
Check if the given Drafty can be represented by plain text.
|
protected static Drafty.Node |
lightEntity(Drafty.Node tree) |
static Drafty |
mention(@NotNull java.lang.String name,
@NotNull java.lang.String uid)
Create a Drafty document consisting of a single mention.
|
static Drafty |
parse(java.lang.String content)
Parse plain text into structured representation.
|
Drafty |
preview(int length)
Shorten Drafty document and strip all entity data leaving just inline styles and entity references.
|
static Drafty |
quote(java.lang.String header,
java.lang.String uid,
Drafty body)
Create a quote of a given Drafty document.
|
@NotNull Drafty |
replyContent(int length,
int maxAttachments)
Prepare Drafty doc for wrapping into QQ as a reply:
- Replace forwarding mention with symbol '➦' and remove data (UID).
|
Drafty |
shorten(int length,
boolean light)
Shorten Drafty document.
|
protected static Drafty.Node |
shortenTree(Drafty.Node tree,
int length,
java.lang.String tail) |
java.lang.String |
toMarkdown()
Mostly for testing: convert Drafty to a markdown string.
|
java.lang.String |
toPlainText() |
@NotNull java.lang.String |
toString() |
protected Drafty.Node |
toTree() |
@NotNull Drafty |
transform(Drafty.Transformer transformer)
Apply custom transformer to Drafty.
|
protected static <T> T |
treeBottomUp(Drafty.Node src,
Drafty.Formatter<T> formatter,
java.util.Stack<java.lang.String> stack) |
protected static @Nullable Drafty.Node |
treeTopDown(@NotNull Drafty.Node node,
@NotNull Drafty.Transformer tr) |
Drafty |
wrapInto(@NotNull java.lang.String style)
Wrap contents of the document into the specified style.
|
public static final java.lang.String MIME_TYPE
public java.lang.String txt
public Drafty.Style[] fmt
public Drafty.Entity[] ent
public Drafty()
public Drafty(java.lang.String content)
protected Drafty(java.lang.String text, Drafty.Style[] fmt, Drafty.Entity[] ent)
public static Drafty fromPlainText(java.lang.String plainText)
plainText
- text assigned without parsing.txt
set to the parameter.public static Drafty parse(java.lang.String content)
content
- content with optional markdown-style markup to parse.public boolean hasEntities(java.lang.Iterable<java.lang.String> types)
public Drafty.Entity[] getEntities()
public java.lang.String[] getEntReferences()
public Drafty insert(int at, @Nullable @Nullable java.lang.String text, @Nullable @Nullable java.lang.String style, @Nullable @Nullable java.util.Map<java.lang.String,java.lang.Object> data)
at
- insertion pointtext
- text to insertstyle
- formatting style.data
- entity datapublic Drafty insertImage(int at, java.lang.String mime, byte[] bits, int width, int height, java.lang.String fname)
at
- location to insert image atmime
- Content-type, such as 'image/jpeg'.bits
- Content as an array of byteswidth
- image width in pixelsheight
- image height in pixelsfname
- name of the file to suggest to the receiver.public Drafty insertImage(int at, @Nullable @Nullable java.lang.String mime, byte[] bits, int width, int height, @Nullable @Nullable java.lang.String fname, @Nullable @Nullable java.net.URI refurl, long size)
at
- location to insert image atmime
- Content-type, such as 'image/jpeg'.bits
- Content as an array of byteswidth
- image width in pixelsheight
- image height in pixelsfname
- name of the file to suggest to the receiver.refurl
- Reference to full/extended image.size
- file size hint (in bytes) as reported by the client.public Drafty attachFile(java.lang.String mime, byte[] bits, java.lang.String fname)
mime
- Content-type, such as 'text/plain'.bits
- Content as an array of bytes.fname
- Optional file name to suggest to the receiver.public Drafty attachFile(java.lang.String mime, java.lang.String fname, java.lang.String refurl, long size)
mime
- Content-type, such as 'text/plain'.fname
- Optional file name to suggest to the receiverrefurl
- reference to content location. If URL is relative, assume current server.size
- size of the attachment (untrusted).protected Drafty attachFile(java.lang.String mime, byte[] bits, java.lang.String fname, java.lang.String refurl, long size)
mime
- Content-type, such as 'text/plain'.fname
- Optional file name to suggest to the receiver.bits
- File content to include inline.refurl
- Reference to full/extended file content.size
- file size hint as reported by the client.public Drafty attachJSON(@NotNull @NotNull java.util.Map<java.lang.String,java.lang.Object> json)
json
- object to attach.public Drafty insertAudio(int at, @NotNull @NotNull java.lang.String mime, byte[] bits, byte[] preview, int duration, @Nullable @Nullable java.lang.String fname, @Nullable @Nullable java.net.URI refurl, long size)
at
- location to insert at.mime
- Content-type, such as 'audio/aac'.bits
- Audio content to include inline.preview
- Amplitude bars to show as preview.duration
- Record duration in milliseconds.fname
- Optional file name to suggest to the receiver.refurl
- Reference to audio content sent out of band.size
- File size hint as reported by the client.this
Drafty document.public Drafty append(@Nullable @Nullable Drafty that)
that
- Drafty document to append to the current document.public Drafty appendLineBreak()
public static Drafty mention(@NotNull @NotNull java.lang.String name, @NotNull @NotNull java.lang.String uid)
name
- is location where the button is inserted.uid
- is the user ID to be mentioned.public Drafty wrapInto(@NotNull @NotNull java.lang.String style)
style
- to wrap document into.protected Drafty insertButton(int at, @Nullable @Nullable java.lang.String title, @Nullable @Nullable java.lang.String id, @NotNull @NotNull java.lang.String actionType, @Nullable @Nullable java.lang.String actionValue, @Nullable @Nullable java.lang.String refUrl)
at
- is the location where the button is inserted.title
- is a button title.id
- is an opaque ID of the button. Client should just return it to the server when the button is clicked.actionType
- is the type of the button, one of 'url' or 'pub'.actionValue
- is the value associated with the action: 'url': URL, 'pub': optional data to add to response.refUrl
- parameter required by URL buttons: url to go to on click.public static Drafty quote(java.lang.String header, java.lang.String uid, Drafty body)
header
- - Quote header (title, etc.).uid
- - UID of the author to mention.body
- - Body of the quoted message.public boolean isPlain()
public <T> T format(@NotNull @NotNull Drafty.Formatter<T> formatter)
formatter
- is an interface with an `apply` method. It's iteratively
applied to every node in the tree.public java.lang.String toMarkdown()
@Nullable protected static @Nullable Drafty.Node treeTopDown(@NotNull @NotNull Drafty.Node node, @NotNull @NotNull Drafty.Transformer tr)
protected static <T> T treeBottomUp(Drafty.Node src, Drafty.Formatter<T> formatter, java.util.Stack<java.lang.String> stack)
protected Drafty.Node toTree()
protected static Drafty.Node shortenTree(Drafty.Node tree, int length, java.lang.String tail)
protected static void attachmentsToEnd(Drafty.Node tree, int maxAttachments)
protected static Drafty.Node lightEntity(Drafty.Node tree)
public java.lang.String toPlainText()
@NotNull public @NotNull java.lang.String toString()
toString
in class java.lang.Object
public boolean equals(java.lang.Object another)
equals
in class java.lang.Object
public Drafty shorten(int length, boolean light)
length
- length in characters to shorten to.public Drafty preview(int length)
length
- length in characters to shorten to.@Nullable public @Nullable Drafty forwardedContent()
@NotNull public @NotNull Drafty replyContent(int length, int maxAttachments)
length-
- length in characters to shorten to.maxAttachments
- - maximum number of attachments to keep.@NotNull public @NotNull Drafty transform(Drafty.Transformer transformer)
transformer
- transformer to apply.