Skip to content

Commit

Permalink
Fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Karasiq committed Aug 11, 2020
1 parent 020ae3c commit 8ffe9b7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ private[components] object PostInlineImage {
private[components] final class PostInlineImage(val base64: String, val imageType: String)(implicit controller: NanoboardController)
extends BootstrapHtmlComponent {

val blobUrl = Blobs.asUrl(Blobs.fromBase64(base64, s"image/$imageType")) // s"data:image/jpeg;base64,$base64"
val expanded = Var(false)

private val styleMod = Rx {
Expand All @@ -30,7 +31,6 @@ private[components] final class PostInlineImage(val base64: String, val imageTyp
}

override def renderTag(md: Modifier*) = {
val blobUrl = Blobs.asUrl(Blobs.fromBase64(base64, s"image/$imageType")) // s"data:image/jpeg;base64,$base64"
img(alt := controller.locale.embeddedImage, src := blobUrl, styleMod.auto, onclick := Callback.onClick(_ expanded() = !expanded.now), md)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.karasiq.nanoboard.frontend.components.post

import scala.scalajs.js
import scala.util.{Failure, Success, Try}

import scalatags.JsDom.all._

Expand Down Expand Up @@ -68,6 +69,8 @@ private[components] object PostRenderer {

case ShortBBCode(name, value)
s"[$name=$value]"

case v s"Error rendering value: $v"
}

def strip(parsed: PostDomValue): String = parsed match {
Expand All @@ -92,82 +95,88 @@ private[components] object PostRenderer {
}

private[components] final class PostRenderer(implicit controller: NanoboardController) {
def render(parsed: PostDomValue): Frag = parsed match {
case PlainText(value)
Linkifier(value)
def render(parsed: PostDomValue): Frag = {
val result = Try[Frag](parsed match {
case PlainText(value)
Linkifier(value)

case PostDomValues(values)
values.map(render)
case PostDomValues(values)
values.map(render)

case BBCode("md", _, value)
PostRenderer.renderMarkdown(PostRenderer.asText(value))

case BBCode("md", _, value)
PostRenderer.renderMarkdown(PostRenderer.asText(value))
case BBCode("b", _, value)
strong(render(value))

case BBCode("b", _, value)
strong(render(value))
case BBCode("i", _, value)
em(render(value))

case BBCode("i", _, value)
em(render(value))
case BBCode("u", _, value)
u(render(value))

case BBCode("u", _, value)
u(render(value))
case BBCode("s", _, value)
s(render(value))

case BBCode("s", _, value)
s(render(value))
case BBCode("g", _, value)
span(controller.style.greenText, render(value))

case BBCode("g", _, value)
span(controller.style.greenText, render(value))
case BBCode("sp" | "spoiler", _, value)
span(controller.style.spoiler, render(value))

case BBCode("sp" | "spoiler", _, value)
span(controller.style.spoiler, render(value))
case ShortBBCode("img" | "xmg", base64)
PostInlineImage(base64)

case ShortBBCode("img" | "xmg", base64)
PostInlineImage(base64)
case BBCode("img", parameters, value)
PostInlineImage(PostRenderer.asText(value), parameters.getOrElse("type", PostInlineImage.defaultType))

case BBCode("img", parameters, value)
PostInlineImage(PostRenderer.asText(value), parameters.getOrElse("type", PostInlineImage.defaultType))
case ShortBBCode("simg", url)
PostExternalImage(url)

case ShortBBCode("simg", url)
PostExternalImage(url)
case BBCode("video", parameters, value)
val url = PostRenderer.asText(value)
PostExternalVideo(url, VideoSource(s"video/${parameters.getOrElse("type", PostExternalVideo.defaultType)}", url))

case BBCode("video", parameters, value)
val url = PostRenderer.asText(value)
PostExternalVideo(url, VideoSource(s"video/${parameters.getOrElse("type", PostExternalVideo.defaultType)}", url))
case ShortBBCode("svid", url)
PostExternalVideo(url, VideoSource(s"video/${PostExternalVideo.defaultType}", url))

case ShortBBCode("svid", url)
PostExternalVideo(url, VideoSource(s"video/${PostExternalVideo.defaultType}", url))
case ShortBBCode("fm", music)
PostFractalMusic(music)

case ShortBBCode("fm", music)
PostFractalMusic(music)
case BBCode("code", parameters, source)
span(raw(PostRenderer.formatCode(PostRenderer.asText(source), parameters.get("lang"))))

case BBCode("code", parameters, source)
span(raw(PostRenderer.formatCode(PostRenderer.asText(source), parameters.get("lang"))))
case BBCode("file", parameters, value)
PostInlineFile(parameters.getOrElse("name", controller.locale.file), PostRenderer.asText(value), parameters.getOrElse("type", ""))

case BBCode("file", parameters, value)
PostInlineFile(parameters.getOrElse("name", controller.locale.file), PostRenderer.asText(value), parameters.getOrElse("type", ""))
case BBCode("link", parameters, value)
a(target := "_blank", href := parameters.getOrElse("url", PostRenderer.asText(value)), render(value))

case BBCode("link", parameters, value)
a(target := "_blank", href := parameters.getOrElse("url", PostRenderer.asText(value)), render(value))
case BBCode("small", _, value)
small(render(value))

case BBCode("small", _, value)
small(render(value))
case BBCode("abbr", parameters, value)
val abbr = tag("abbr")
abbr(title := parameters.getOrElse("title", ""), render(value))

case BBCode("abbr", parameters, value)
val abbr = tag("abbr")
abbr(title := parameters.getOrElse("title", ""), render(value))
case BBCode("mark", _, value)
val mark = tag("mark")
mark(render(value))

case BBCode("mark", _, value)
val mark = tag("mark")
mark(render(value))
case BBCode("quote", parameters, value)
blockquote(
if (parameters.contains("reverse")) "blockquote-reverse".addClass else (),
p(render(value)),
parameters.get("title").map(footer(_))
)
})

case BBCode("quote", parameters, value)
blockquote(
if (parameters.contains("reverse")) "blockquote-reverse".addClass else (),
p(render(value)),
parameters.get("title").map(footer(_))
)
result match {
case Success(frag) frag

// Unknown
case value
PostRenderer.asText(value)
case Failure(exception)
exception.printStackTrace()
PostRenderer.asText(parsed)
}
}
}
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 0.13.16
sbt.version = 0.13.18

0 comments on commit 8ffe9b7

Please sign in to comment.