Skip to content
Permalink
Browse files

Migration of deltas to delta package

  • Loading branch information
darkfrog26 committed Feb 5, 2020
1 parent dfcda4f commit 420e335a5a01f90c1bec7dff752ec08b819db025
Showing with 186 additions and 135 deletions.
  1. +1 −1 app/jvm/src/main/scala/io/youi/app/Page.scala
  2. +2 −1 app/jvm/src/main/scala/io/youi/app/ServerApplication.scala
  3. +2 −1 app/jvm/src/main/scala/io/youi/app/package.scala
  4. +1 −0 optimizer/src/main/scala/io/youi/optimizer/HTMLOptimizer.scala
  5. +1 −1 server/src/main/scala/io/youi/http/HttpConnection.scala
  6. +1 −1 server/src/main/scala/io/youi/server/dsl/package.scala
  7. +2 −1 server/src/main/scala/io/youi/server/handler/HttpHandlerBuilder.scala
  8. +1 −1 server/src/main/scala/io/youi/server/handler/ProxyCache.scala
  9. +0 −128 stream/src/main/scala/io/youi/stream/Delta.scala
  10. +2 −0 stream/src/main/scala/io/youi/stream/StreamableHTML.scala
  11. +26 −0 stream/src/main/scala/io/youi/stream/delta/Delta.scala
  12. +15 −0 stream/src/main/scala/io/youi/stream/delta/Grouped.scala
  13. +9 −0 stream/src/main/scala/io/youi/stream/delta/InsertAfter.scala
  14. +9 −0 stream/src/main/scala/io/youi/stream/delta/InsertBefore.scala
  15. +9 −0 stream/src/main/scala/io/youi/stream/delta/InsertFirstChild.scala
  16. +9 −0 stream/src/main/scala/io/youi/stream/delta/InsertLastChild.scala
  17. +23 −0 stream/src/main/scala/io/youi/stream/delta/Processor.scala
  18. +22 −0 stream/src/main/scala/io/youi/stream/delta/Repeat.scala
  19. +9 −0 stream/src/main/scala/io/youi/stream/delta/Replace.scala
  20. +24 −0 stream/src/main/scala/io/youi/stream/delta/ReplaceAttribute.scala
  21. +9 −0 stream/src/main/scala/io/youi/stream/delta/ReplaceContent.scala
  22. +9 −0 stream/src/main/scala/io/youi/stream/delta/Template.scala
@@ -4,7 +4,7 @@ import io.youi.http.HttpConnection
import io.youi.http.content.Content
import io.youi.server.handler.{CachingManager, HttpProcessor}
import io.youi.server.validation.Validator
import io.youi.stream._
import io.youi.stream.delta.Delta

import scala.concurrent.Future

@@ -7,11 +7,12 @@ import io.youi.http.content.{Content, FileContent, FormDataContent, StringConten
import io.youi.net.{ContentType, URL}
import io.youi.server.Server
import io.youi.server.handler.{CachingManager, HttpHandler, HttpHandlerBuilder, SenderHandler}
import io.youi.stream.{Delta, HTMLParser, Selector}
import io.youi.stream.{HTMLParser, Selector}
import io.youi.{JavaScriptError, JavaScriptLog, Priority, http}
import net.sf.uadetector.UserAgentType
import net.sf.uadetector.service.UADetectorServiceFactory
import io.youi.stream._
import io.youi.stream.delta.Delta
import io.youi.util.Time
import profig.{JsonUtil, Profig}
import reactify.Var
@@ -2,7 +2,8 @@ package io.youi

import io.youi.http.HttpConnection
import io.youi.server.dsl.{ConnectionFilter, DeltaKey, FilterResponse}
import io.youi.stream.{Selector, Delta}
import io.youi.stream.delta.Delta
import io.youi.stream.Selector

import scala.concurrent.Future

@@ -6,6 +6,7 @@ import java.net.URL
import com.roundeights.hasher.Hasher
import io.youi.stream._
import io.youi.stream._
import io.youi.stream.delta.Delta

import scala.collection.mutable.ListBuffer

@@ -1,7 +1,7 @@
package io.youi.http

import io.youi.server.{Server, WebSocketListener}
import io.youi.stream.Delta
import io.youi.stream.delta.Delta
import io.youi.{MapStore, Store}

case class HttpConnection(server: Server,
@@ -9,7 +9,7 @@ import io.youi.net.{ContentType, IP, Path, URLMatcher}
import io.youi.server.handler._
import io.youi.server.rest.Restful
import io.youi.server.validation.{ValidationResult, Validator}
import io.youi.stream.Delta
import io.youi.stream.delta.Delta

import scala.concurrent.Future
import scala.language.implicitConversions
@@ -11,7 +11,8 @@ import io.youi.http.content.{Content, StringContent}
import io.youi.net.{ContentType, Path, URL, URLMatcher}
import io.youi.server.Server
import io.youi.server.validation.{ValidationResult, Validator}
import io.youi.stream.{Delta, HTMLParser, Selector}
import io.youi.stream.delta.Delta
import io.youi.stream.{HTMLParser, Selector}

import scala.concurrent.Future
import scribe.Execution.global
@@ -6,7 +6,7 @@ import java.nio.file.{Path, Paths}
import io.youi.http.content.Content
import io.youi.http.{HttpConnection, HttpStatus}
import io.youi.net.ContentType
import io.youi.stream.Delta
import io.youi.stream.delta.Delta
import io.youi.stream.Selector._
import io.youi.stream._

This file was deleted.

@@ -4,6 +4,8 @@ import java.io.File
import java.nio.channels.FileChannel
import java.nio.file.StandardOpenOption

import io.youi.stream.delta.Delta

class StreamableHTML(file: File, cacheBuilder: CacheBuilder) {
private var cache = cacheBuilder.buildCache()

@@ -0,0 +1,26 @@
package io.youi.stream.delta

import io.youi.stream.{HTMLStream, Selector, Tag}

trait Delta {
def selector: Selector
def apply(streamer: HTMLStream, tag: Tag.Open): Unit
}

object Delta {
def Replace(selector: Selector, content: => String): Replace = new Replace(selector, () => content)
def Process(selector: Selector,
replace: Boolean,
onlyOpenTag: Boolean,
processor: (Tag.Open, String) => String,
closeTagProcessor: Option[(Tag.Open, Tag.Close, String) => String] = None): Processor = new Processor(selector, replace, onlyOpenTag, processor, closeTagProcessor)
def InsertBefore(selector: Selector, content: => String): InsertBefore = new InsertBefore(selector, () => content)
def InsertFirstChild(selector: Selector, content: => String): InsertFirstChild = new InsertFirstChild(selector, () => content)
def ReplaceContent(selector: Selector, content: => String): ReplaceContent = new ReplaceContent(selector, () => content)
def ReplaceAttribute(selector: Selector, attributeName: String, content: => String): ReplaceAttribute = new ReplaceAttribute(selector, attributeName, () => content)
def InsertLastChild(selector: Selector, content: => String): InsertLastChild = new InsertLastChild(selector, () => content)
def InsertAfter(selector: Selector, content: => String): InsertAfter = new InsertAfter(selector, () => content)
def Repeat[Data](selector: Selector, data: List[Data], changes: Data => List[Delta]): Repeat[Data] = new Repeat(selector, data, changes)
def Template(selector: Selector, deltas: List[Delta]): Template = new Template(selector, deltas)
def Grouped(selector: Selector, deltas: Delta*): Grouped = new Grouped(selector, deltas.toList)
}
@@ -0,0 +1,15 @@
package io.youi.stream.delta

import io.youi.stream.{HTMLStream, Selector, Tag}

class Grouped(val selector: Selector, deltas: List[Delta]) extends Delta {
override def apply(streamer: HTMLStream, tag: Tag.Open): Unit = {
deltas.zipWithIndex.foreach {
case (d, index) => {
streamer.grouped(index) {
d(streamer, tag)
}
}
}
}
}
@@ -0,0 +1,9 @@
package io.youi.stream.delta

import io.youi.stream.{HTMLStream, Selector, Tag}

class InsertAfter(val selector: Selector, val content: () => String) extends Delta {
override def apply(streamer: HTMLStream, tag: Tag.Open): Unit = {
streamer.insert(tag.close.map(_.end).getOrElse(tag.end), content())
}
}
@@ -0,0 +1,9 @@
package io.youi.stream.delta

import io.youi.stream.{HTMLStream, Selector, Tag}

class InsertBefore(val selector: Selector, val content: () => String) extends Delta {
override def apply(streamer: HTMLStream, tag: Tag.Open): Unit = {
streamer.insert(tag.start, content())
}
}
@@ -0,0 +1,9 @@
package io.youi.stream.delta

import io.youi.stream.{HTMLStream, Selector, Tag}

class InsertFirstChild(val selector: Selector, val content: () => String) extends Delta {
override def apply(streamer: HTMLStream, tag: Tag.Open): Unit = {
streamer.insert(tag.end, content())
}
}
@@ -0,0 +1,9 @@
package io.youi.stream.delta

import io.youi.stream.{HTMLStream, Selector, Tag}

class InsertLastChild(val selector: Selector, val content: () => String) extends Delta {
override def apply(streamer: HTMLStream, tag: Tag.Open): Unit = {
streamer.insert(tag.close.get.start, content())
}
}
@@ -0,0 +1,23 @@
package io.youi.stream.delta

import io.youi.stream.{HTMLStream, Selector, Tag}

class Processor(val selector: Selector,
replace: Boolean,
onlyOpenTag: Boolean,
processor: (Tag.Open, String) => String,
closeTagProcessor: Option[(Tag.Open, Tag.Close, String) => String]) extends Delta {
override def apply(streamer: HTMLStream, tag: Tag.Open): Unit = {
val end = if (onlyOpenTag) {
tag.end
} else {
tag.close.map(_.end).getOrElse(tag.end)
}
streamer.process(tag.start, end, processor.curried(tag), replace = replace)
closeTagProcessor.foreach { processor =>
tag.close.foreach { closeTag =>
streamer.process(closeTag.start, closeTag.end, processor.curried(tag)(closeTag))
}
}
}
}
@@ -0,0 +1,22 @@
package io.youi.stream.delta

import io.youi.stream.{HTMLStream, Selector, Tag}

class Repeat[Data](val selector: Selector, data: List[Data], deltas: Data => List[Delta]) extends Delta {
override def apply(streamer: HTMLStream, tag: Tag.Open): Unit = {
data.zipWithIndex.foreach {
case (d, index) => {
streamer.grouped(index) {
streamer.reposition(tag.start)
deltas(d).foreach { delta =>
val tags = delta.selector.lookup(streamer.streamable)
tags.foreach { tag =>
delta(streamer, tag)
}
}
streamer.insert(tag.close.map(_.end).getOrElse(tag.end), "")
}
}
}
}
}
@@ -0,0 +1,9 @@
package io.youi.stream.delta

import io.youi.stream.{HTMLStream, Selector, Tag}

class Replace(val selector: Selector, val content: () => String) extends Delta {
override def apply(streamer: HTMLStream, tag: Tag.Open): Unit = {
streamer.replace(tag.start, tag.close.map(_.end).getOrElse(tag.end), content())
}
}
@@ -0,0 +1,24 @@
package io.youi.stream.delta

import io.youi.stream.{HTMLStream, Selector, Tag}

class ReplaceAttribute(val selector: Selector, attributeName: String, val content: () => String) extends Delta {
private val AttributeRegex = s"""$attributeName="(.*?)"""".r

override def apply(streamer: HTMLStream, tag: Tag.Open): Unit = {
val attribute = s"""$attributeName="${content()}""""
streamer.process(tag.start, tag.end, (block: String) => {
AttributeRegex.replaceAllIn(block, replacer => {
attribute
}) match {
case s if s == block => { // Not found, we need to add it
val selfClosing = block.endsWith("/>")
val left = block.substring(0, block.length - (if (selfClosing) 2 else 1))
val right = if (selfClosing) "/>" else ">"
s"$left $attribute$right"
}
case s => s // Replaced, just return it
}
})
}
}
@@ -0,0 +1,9 @@
package io.youi.stream.delta

import io.youi.stream.{HTMLStream, Selector, Tag}

class ReplaceContent(val selector: Selector, val content: () => String) extends Delta {
override def apply(streamer: HTMLStream, tag: Tag.Open): Unit = {
streamer.replace(tag.end, tag.close.get.start, content())
}
}

0 comments on commit 420e335

Please sign in to comment.
You can’t perform that action at this time.