Permalink
Browse files

shows from all around

  • Loading branch information...
1 parent 0cb47d3 commit f404890b7171529c1d32ad2ea26d05308d5f6972 @softprops committed Jan 14, 2012
@@ -1,30 +1,26 @@
package pictureshow
-trait Config { self: IO with Logging =>
+trait Config { self: Resolver /*IO*/ =>
import scala.util.parsing.json.JSON
- val configName = "conf.js"
/** title of show */
def showTitle = provided.title.getOrElse("picture show")
/** list of sections to render */
def sections = provided.sections.getOrElse(Nil)
case class Show(title: Option[String], sections: Option[List[String]])
- private val provided = {
- slurp(configName) map { contents =>
- lazy val json = JSON.parseFull(contents)
-
- def extract[T](name: String)(f: Any => T) = json flatMap {
- case m: Map[String, Any] if(m isDefinedAt name) => Some(f(m(name)))
- case e => println(e); None
- }
- json match {
- case None => sys.error("Invalid json config %s: %s" format(configName, contents))
- case _ => Show(extract("title") { _.toString }, extract("sections") { v => v.asInstanceOf[List[String]] })
- }
- } getOrElse {
- sys.error("Invalid json in file %s" format configName)
+ private val provided = {
+ lazy val json = JSON.parseFull(configuration)
+ def extract[T](name: String)(f: Any => T) = json flatMap {
+ case m: Map[String, Any] if(m isDefinedAt name) => Some(f(m(name)))
+ case e => None
+ }
+ json match {
+ case None => sys.error("malformed configuration '%s'" format configuration)
+ case _ => Show(
+ extract("title") { _.toString },
+ extract("sections") { v => v.asInstanceOf[List[String]] })
}
}
}
@@ -1,10 +1,9 @@
package pictureshow
-trait IO { self: Resolver =>
+object IO {
import java.net.URL
- def exists(path: String) = slurp(path).isDefined
- def slurp(path: String) = try {
- Some(scala.io.Source.fromURL(new URL(loadPath, path), "utf-8").mkString(""))
+ def slurp(url: URL) = try {
+ Some(scala.io.Source.fromURL(url, "utf-8").mkString(""))
} catch {
case e: java.io.FileNotFoundException => None
}
@@ -1,19 +1,19 @@
package pictureshow
-trait Markup { self: IO with Resolver with Config with Logging =>
+trait Markup { self: Resolver with Config =>
import pamflet.PamfletDiscounter._
- import java.net.URL
/** combine all js assets */
- def combineJs = ("js/custom.js" :: Nil) filter exists
+ def combineJs = ("js/custom.js" :: Nil) filter exists /*IO*/
/** combine all css assets */
- def combineCss = ("css/custom.css" :: Nil) filter exists
+ def combineCss = ("css/custom.css" :: Nil) filter exists /*IO*/
/** loads and processes all markdown from configured sections */
def mkSlides = {
((new xml.NodeBuffer, 0) /: sections) ((a, s) => {
- val files = slurp("%s/%s.md" format (s, s))
- if(files.isEmpty) log("no file(s) at path %s/%s.md" format(s, s))
- val fileRes = (((new scala.xml.NodeBuffer, a._2) /: files)((m, f) => {
+ // FIXME gists can't have paths with depth
+ val file = resolve("%s/%s.md" format (s, s))
+ if(file.isEmpty) log("no file(s) at path %s/%s.md" format(s, s))
+ val fileRes = (((new scala.xml.NodeBuffer, a._2) /: file)((m, f) => {
val mdRes = parse(f, m._2)
(m._1 &+ mdRes._1, mdRes._2)
}))
@@ -1,7 +1,9 @@
package pictureshow
-class Renderer(root: java.net.URL) extends Resolver(root)
- with Config with IO with Markup with Templates
+class Renderer(uri: java.net.URI) extends Resolved(uri)
+ with Config
+ with Markup
+ with Templates
with Logging {
def renderDefault = render(css(combineCss), mkSlides, js(combineJs))
@@ -1,5 +1,79 @@
package pictureshow
-class Resolver(path: java.net.URL) {
- def loadPath = path
-}
+import java.net.URI
+import scala.util.parsing.json.JSON
+import dispatch._
+
+trait Resolver {
+ def configuration: String
+ def resolve(p: String): Option[String]
+ def exists(p: String): Boolean
+}
+
+class GistGitResolver(uri: URI) extends Resolver {
+
+ lazy val configuration = "todo"
+ def resolve(p: String) = Some("todo")
+ def exists(p: String) = false
+}
+
+class GistHttpResolver(id: String) extends Resolver {
+ private def gist = Http(:/("api.github.com").secure / "gists" / id as_str)
+ private lazy val files: Map[String, String] = JSON.parseFull(gist).get match {
+ case g: Map[String, Any] => g("files") match {
+ case fs: Map[String, Any] => fs.map {
+ case (k, meta: Map[String, String]) =>
+ (k, meta("content"))
+ }
+ }
+ case s => sys.error("unexpected response structure %s" format s)
+ }
+ lazy val configuration = files("conf.json")
+ def resolve(p: String) = files.get(p.split('/').last)
+ def exists(p: String) = files isDefinedAt p.split('/').last
+}
+
+class FileSystemResolver(uri: URI) extends Resolver {
+ import java.io.File
+ def configuration = IO.slurp(new java.net.URL(uri.toURL, "conf.js")).get
+ def resolve(p: String) = IO.slurp(new java.net.URL(uri.toURL, p))
+ def exists(p: String) = new File(uri.toURL.getFile, p).exists
+}
+
+// resolves local (filesystem) & remote (git/gist) shows
+class Resolved(uri: URI) extends Resolver {
+
+ object GistHttp {
+ val ID = """^/(\d+)""".r
+ def unapply(uri: URI) = (uri.getScheme, uri.getHost, uri.getPath) match {
+ case ("https", "gist.github.com", ID(id)) => Some(id)
+ case _ => None
+ }
+ }
+
+ object GistGit {
+ def unapply(uri: URI) = (uri.getScheme, uri.getHost) match {
+ case ("git", "gist.github.com") => Some(uri)
+ case _ => None
+ }
+ }
+
+ object FileSystem {
+ def unapply(uri: URI) = uri.getScheme match {
+ case "file" => Some(uri)
+ case _ => None
+ }
+ }
+
+ private lazy val adapted = uri match {
+ case GistHttp(uri) => new GistHttpResolver(uri)
+ case GistGit(uri) => new GistGitResolver(uri)
+ case FileSystem(uri) => new FileSystemResolver(uri)
+ case uh => sys.error("can not adapt to resolving show from uri %s" format uh)
+ }
+
+ def configuration = adapted.configuration
+ def resolve(p: String) = adapted.resolve(p)
+ def exists(p: String) = adapted.exists(p)
+
+}
@@ -26,15 +26,15 @@ object Offline {
} map { case (u, t) => u -> new File(parent, "assets/" + t) }
/** String => String source/target mappings for show assets. */
def showAssetMappings(fromF: File)(toF: File) = {
- val assets = (new Renderer(fromF.toURI.toURL)).sections ++ someFile(fromF, "js") ++ someFile(fromF, "css")
+ val assets = (new Renderer(fromF.toURI)).sections ++ someFile(fromF, "js") ++ someFile(fromF, "css")
val assetPaths = assets.map(s => new File(fromF, s).getAbsolutePath)
assetPaths.flatMap(sp => Files.ls(sp)(_ => true)) map { p =>
p -> p.replaceFirst("\\Q%s\\E".format(fromF.getAbsolutePath), toF.getAbsolutePath)
}
}
/** Render a show as if it were being delivered over the wire. */
def renderShow(fromF: File) =
- (new Renderer(fromF.toURI.toURL)).renderDefault
+ (new Renderer(fromF.toURI)).renderDefault
/** Build an offline version of a show. */
def apply(from: String, assets: Iterator[java.net.URL], to: String) = {
View
@@ -8,7 +8,7 @@ object Build extends sbt.Build {
lazy val standardSettings = Defaults.defaultSettings ++ Seq(
organization := "me.lessis",
version := "0.1.0-SNAPSHOT",
- libraryDependencies += knockoff,
+ libraryDependencies ++= Seq(knockoff, dispatch),
scalacOptions += "-deprecation"
)
@@ -23,7 +23,7 @@ object Build extends sbt.Build {
"PictureShow Core",
file("core"),
settings = standardSettings ++ Seq(
- libraryDependencies ++= Seq(codec, specs)
+ libraryDependencies ++= Seq(codec, specs)
)
)
@@ -60,5 +60,6 @@ object Build extends sbt.Build {
val uf_version = "0.5.3"
val uff = "net.databinder" %% "unfiltered-filter" % uf_version
val ufj = "net.databinder" %% "unfiltered-jetty" % uf_version
+ val dispatch = "net.databinder" %% "dispatch-http" % "0.8.7"
}
}
@@ -3,9 +3,9 @@ package pictureshow
import unfiltered._
import unfiltered.request._
import unfiltered.response._
-import java.net.URL
+import java.net.URI
-class Projector(path: URL) extends Renderer(path) with unfiltered.filter.Plan {
+class Projector(uri: URI) extends Renderer(uri) with unfiltered.filter.Plan {
def intent = {
case Path("/") => HtmlContent ~>
ResponseString(renderDefault)
@@ -40,7 +40,7 @@ object Server extends Logging {
} else if (!new JFile(show, "conf.js").exists) {
Left("conf.js not found under @ `%s`." format show.toString)
} else {
- val projector = new Projector(show.toURI.toURL)
+ val projector = new Projector(/*show.toURI*/new java.net.URI("https://gist.github.com/1289003"))
if (projector.sections.isEmpty) {
Left("Show content not found @ `%s`. conf.js" format show.toString)
} else {

0 comments on commit f404890

Please sign in to comment.