Skip to content

Commit

Permalink
Finish documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
wingio committed Dec 8, 2023
1 parent 2c5f570 commit 7766625
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
22 changes: 22 additions & 0 deletions app/src/main/java/xyz/wingio/dimett/ast/Renderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import xyz.wingio.syntakts.compose.rememberAsyncRendered
import xyz.wingio.syntakts.markdown.addBasicMarkdownRules
import xyz.wingio.syntakts.syntakts

/**
* Renders a formatted version of [text]
*
* @param text Text to parse and format
* @param emojiMap Used to insert custom emoji into the text
* @param mentionMap All the users mentioned in the post
* @param actionHandler Callback that is passed the actions name
*/
@Composable
fun Syntakts<DefaultRenderContext>.render(
text: String,
Expand All @@ -21,6 +29,11 @@ fun Syntakts<DefaultRenderContext>.render(
context = rememberRenderContext(emojiMap, mentionMap, actionHandler)
)

/**
* Creates a remembered version of [DefaultRenderContext]
*
* @see DefaultRenderContext
*/
@Composable
fun rememberRenderContext(
emojiMap: Map<String, String>,
Expand All @@ -35,6 +48,9 @@ fun rememberRenderContext(
}
}

/**
* Contains all the rules for rendering post content
*/
val DefaultSyntakts = syntakts {
addHashtagRule()
addMentionRule()
Expand All @@ -43,13 +59,19 @@ val DefaultSyntakts = syntakts {
addUnicodeEmojiRule()
}

/**
* Contains the rules for rendering styled string resources
*/
val StringSyntakts = syntakts {
addUrlRule()
addClickableRule()
addBasicMarkdownRules()
addUnicodeEmojiRule()
}

/**
* Only contains rules necessary for emotes and Twemoji
*/
val EmojiSyntakts = syntakts {
addEmojiRule()
addUnicodeEmojiRule()
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/java/xyz/wingio/dimett/ast/Rules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import xyz.wingio.syntakts.compose.style.toSyntaktsColor
import xyz.wingio.syntakts.style.Style

const val urlRegex =
"https?:\\/\\/([\\w+?]+\\.[\\w+]+)([a-zA-Z0-9\\~\\!\\@\\#\\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?"
"https?:\\/\\/([\\w+?]+\\.[\\w+]+)([a-zA-Z0-9\\~\\!\\@\\#\\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?" // https://example.com

const val hyperlinkRegex =
"\\[(.+?)\\]\\(($urlRegex)\\)"
"\\[(.+?)\\]\\(($urlRegex)\\)" // [link](https://example.com)

const val clickableRegex =
"\\[(.+?)\\]\\{(.+?)\\}"
"\\[(.+?)\\]\\{(.+?)\\}" // [some text]{onSomeAction}

const val emojiRegex =
":(.+?):"
":(.+?):" // :shortcode:

const val mentionRegex =
"@(\\S+?)\\b(@(\\S+)\\b)?"
"@(\\S+?)\\b(@(\\S+)\\b)?" // @user or @[email protected]

const val hashtagRegex =
"#(.+?)\\b"
"#(.+?)\\b" // #sometext

fun Syntakts.Builder<DefaultRenderContext>.addUrlRule() {
rule(urlRegex) { result, context ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.UriHandler

data class DefaultRenderContext(
val emojiMap: Map<String, String>,
val mentionMap: Map<String, String>,
val emojiMap: Map<String, String>, // shortcode: url
val mentionMap: Map<String, String>, // username to id
val linkColor: Color,
val uriHandler: UriHandler,
val clickActionHandler: (String) -> Unit
val clickActionHandler: (String) -> Unit // Is passed the action name (Ex. onUserClick)
)
2 changes: 1 addition & 1 deletion app/src/main/java/xyz/wingio/dimett/di/HttpModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ val httpModule = module {
defaultRequest {
header(
HttpHeaders.UserAgent,
"Dimett/${BuildConfig.APPLICATION_ID} v${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})${if (BuildConfig.DEBUG) " - Debug" else ""}"
"Dimett/${BuildConfig.APPLICATION_ID} v${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})" // Dimett/xyz.wingio.dimett v1.0.0 (1)
)
}
install(Logging) {
Expand Down

0 comments on commit 7766625

Please sign in to comment.