Permalink
Browse files

Moved samples to the main page. Simplified things.

Probably should start using lift actually. Not right now though.
  • Loading branch information...
1 parent 6847cb0 commit a0647a6a53df45f1182e5c6105377ef3fcab44d4 @vpatryshev committed Oct 15, 2011
View
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+ <component name="HighlightingAdvisor">
+ <option name="SUGGEST_TYPE_AWARE_HIGHLIGHTING" value="false" />
+ <option name="TYPE_AWARE_HIGHLIGHTING_ENABLED" value="true" />
+ </component>
+</project>
+
@@ -0,0 +1,8 @@
+package org.presheaf
+
+object Layouts {
+ def walkDiagonally(width: Int, height: Int) = {
+ for (i <- 0 to width + height - 1;
+ j <- 0 to width + height - 1) yield (i, j)
+ }
+}
@@ -1,62 +0,0 @@
-package org.presheaf
-
-import org.presheaf.DiagramRenderer._
-import org.presheaf.HtmlSnippets._
-
-import javax.servlet.http._
-import scala.xml._
-
-class DiagramPage extends PresheafServlet {
- val sample = "X \\ar@/_2pc/[rr]_{f;g} \\ar[r]^f &Y \\ar[r]^g &Z\\"
-
- def page(title: String, diagram: String, ns: Seq[Node]) = {
- <html>
- <head>
- <title>{ title }</title>
- </head>
- <body>
- <h1>{ title }</h1>
- { ns }
- <form>
- <select name="format">
- <option value="xy">xypic</option>
- <option value="graph">graph</option>
- <option value="guess">ascii art</option>
- </select>
- <textarea name="in" cols="60" rows="10">{ Text(diagram) }</textarea>
- <input type="submit" value="Submit" />
- </form>
- <p>
- <a href="http://ctan.org/tex-archive/macros/generic/diagrams/xypic/xy/doc/xyguide.pdf">This pdf</a> tells in details how to write diagrams in xypic format.
- </p>
-
- <a href="samples">Samples</a><br/>
-
- { signature }
- </body>
- </html>
- }
-
- override def doGetXML(req:HttpServletRequest) = {
- configure(req.getSession.getServletContext)
-// val format = notNull(req.getParameter("format"), "xy")
- val param = req.getParameter("in")
-
- if (param == null) {
- page("Let's try a diagram",
- sample,
- <p>enter something below</p>)
- } else {
- val (id, source, imgRef, pdfRef, logs) = process(req)
- val img = <img/> % fileAsAttr("src", imgRef)
- val pdf = <a>pdf</a> % fileAsAttr("href", pdfRef)
- page("Here's your diagram",
- source,
- <p>"<b>{ source }</b>"</p>
- <p>{ img }</p>
- <p>{ pdf }</p>
- <p>{ logs }</p>)
- }
- }
-
-}
@@ -13,7 +13,7 @@ class DiagramSamples extends PresheafServlet {
</head>
<body>
<h1>{ title }</h1>
- <p>{ samples }</p>
+ <p>{ samplesHtml }</p>
<p>
<a href="http://ctan.org/tex-archive/macros/generic/diagrams/xypic/xy/doc/xyguide.pdf">This pdf</a> tells in details how to write diagrams in xypic format.
</p>
@@ -24,7 +24,7 @@ class DiagramSamples extends PresheafServlet {
def oneSample(xy: String) =
<tr>
- <td>{ img("dws?out=png&in=" + DiagramRenderer.encode(xy)) }</td>
+ <td>{ img("dws?op=aspng&format=xy&in=" + DiagramRenderer.encode(xy)) }</td>
<td>{ xy }</td>
</tr>
@@ -33,21 +33,21 @@ class DiagramSamples extends PresheafServlet {
page("XY Diagram Samples")
}
- def samples() = {
+ def samplesHtml = {
<table border="1">
<tr>
<th>diagram</th>
<th>xypic source</th>
</tr>
- { DiagramSamples.snippets map oneSample }
+ { DiagramSamples.samples map oneSample }
</table>
}
}
object DiagramSamples {
- def snippets = {
+ def samples = {
((List[String](),"") /: Res.read("/samples.txt").getLines()) (
(accumulator: (List[String], String), line) => line match {
case "" => accumulator._2 match {
@@ -4,66 +4,68 @@ import org.presheaf.HtmlSnippets._
import javax.servlet.http._
import xml.Node
import java.io.File
-import java.awt.RenderingHints.Key
class DiagramService extends PresheafServlet {
val xyError = ".*Xy-pic error:(.*)\\\\xyerror.*".r
-/*
- def page(diagram: String, imgRef: String, pdfRef: String, logs: Seq[Node]) = {
- <xml>
- <diagram>
- <source>{ diagram }</source>
- <image>{ imgRef }</image>
- <pdf>{ pdfRef }</pdf>
- <logs>{ logs }</logs>
- <version>{ version }</version>
- </diagram>
- </xml>
- }
-*/
+
val Q = "\""
def quote(s: String) = Q + s.replaceAll(Q, "").replaceAll("\\\\", "\\\\\\\\") + Q
- def attr(nvp: (String,Object)) = nvp._1 + ":" + quote(nvp._2.toString)
- def json(map: Map[String, Object]) = map.map(attr _).mkString("{", ",", "}")
+ def json(s: String): String = quote(s)
+ def json(nvp: (String,_)): String = json(nvp._1) + ":" + json(nvp._2.toString)
+ def json(map: Map[String, _]): String = map.map(json(_)).mkString("{", ",", "}")
+ def json(seq: Iterator[String]): String = seq.map(json).mkString("[", ",\n", "]")
+// def json(seq: Iterator[Map[String, _]]): String = seq.map(json).mkString("[", ",\n", "]")
+ def json(seq: Iterable[String]): String = json(seq.iterator)
+
+ def errorLog(logs: Iterable[Node]) = {
+ val fullLog = logs.mkString("<br/>").replaceAll("\\\\", "\\\\").replaceAll("\"", "\\\"")
+ fullLog match {
+ case xyError(msg) =>
+ Map(
+ "error" -> msg,
+ "version" -> version)
+ case _ =>
+ Map(
+ "error" -> fullLog,
+ "version" -> version)
+
+ }
+ }
+
+ def produce(req:HttpServletRequest, diagram:String): String = {
+ //return Map("error" -> "Sorry, this is broken now, working on it - Vlad, 1:05pm (PDT), 10/14/2011")
+ val (id, source, img, pdf, logs) : (String, String, File, File, Iterable[Node]) = process(req, diagram, req.getParameter("opt"))
+ json(
+ if (logs.isEmpty) {
+ Map(
+ "id" -> id,
+ "source" -> quote(source),
+ "version" -> version)
+ }
+ else {
+ errorLog(logs)
+ }
+ )
+ }
override def doGet(req:HttpServletRequest, res:HttpServletResponse) : Unit = {
res.setContentType("text/html")
try {
- val (id, source, img, pdf, logs) : (String, String, File, File, Iterable[Node]) = process(req)
- req.getParameter("out") match {
- case "png" =>
+
+ req.getParameter("op") match {
+ case "samples" => res.getWriter.print(DiagramSamples.samples.map(produce(req, _)).mkString("[", ",\n", "]"))
+
+ case "aspng" =>
res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY)
- res.setHeader("Location", ref(img))
+ res.setHeader("Location", ref(process(req)._3))
- case "pdf" =>
+ case "aspdf" =>
res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY)
- res.setHeader("Location", ref(pdf))
+ res.setHeader("Location", ref(process(req)._4))
case _ =>
- res.getWriter.print(json(
- if (logs.isEmpty) {
- Map(
- "id" -> id,
- "source" -> source,
- "imageUrl" -> ref(img),
- "pdfUrl" -> ref(pdf),
- "version" -> version)
- }
- else {
- val fullLog = logs.mkString("<br/>").replaceAll("\\\\", "\\\\").replaceAll("\"", "\\\"")
- fullLog match {
- case xyError(msg) =>
- Map(
- "error" -> msg,
- "version" -> version)
- case _ =>
- Map(
- "error" -> fullLog,
- "version" -> version)
-
- }
- }
- ))
+ val result = produce(req, req.getParameter("in"))
+ res.getWriter.print(result)
}
} catch {
case bd: BadDiagram => {
@@ -72,34 +74,10 @@ class DiagramService extends PresheafServlet {
}
case e: Throwable => {
println("Diagram service: an exception")
- println(e)
e.printStackTrace
res.getWriter.print(json(Map("error" -> e.getMessage)))
- e.printStackTrace(); res.sendError(500, "Error while processing the diagram: " + e.getMessage)
+ res.sendError(500, "Error while processing the diagram: " + e.getMessage)
}
}
}
-/*
-def cached[K,V](f: K => V) : K => V = {
- val cache = new scala.collection.mutable.HashMap[K,V]
- k => cache.getOrElseUpdate(k, f(k))
- }
-
-def linear(n: Int) = ((n + 1) / 2.
-
-val best: Int => (Double, Int) = cached((n: Int) =>
- if (n < 4) (linear(n), 0) else
- (for (k <- 2 to (n/2)) yield
- (1 + linear(k) * k / n + best(n-k)._1 * (n-k) / n, k)).min)
-
-val theirs = List(14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 1)
-
-def forSequence(s: List[Int]) = ((0., 0) /: s.reverse) ((a,b) => { val c = a._2 + b; (a._1 * a._2 / c + 1 + linear(b) * b / c, c)})
-
-def bestSteps(height: Int): (Double, List[Int]) = {
- val first = best(height)
- (first._1, if (height < 4) Nil
- else first._2 :: bestSteps(height - first._2)._2)
-}
-*/
}
@@ -4,7 +4,6 @@ import scala.xml._
import xml.UnprefixedAttribute
object HtmlSnippets {
-// def img(src: String) = <img/> % new UnprefixedAttribute("src", src, Null)
def img(src: String) = <img src={src}/>
def buildNo: String = {
@@ -16,16 +16,25 @@ abstract class PresheafServlet extends HttpServlet {
def fileAsAttr(attr: String, file: File) = new UnprefixedAttribute(attr, ref(file), Null)
def process(req:HttpServletRequest) : (String, String, File, File, Iterable[Node]) = {
- val diagram = req.getParameter("in")
+ process(req, req.getParameter("in"), req.getParameter("opt"))
+ }
+
+ def process(req:HttpServletRequest, diagram: String, opt: String) : (String, String, File, File, Iterable[Node]) = {
+ if (diagram == null || diagram.isEmpty) throw new BadDiagram("no diagram to render")
println("Rendering diagram \"" + diagram + "\"")
// val context = req.getSession.getServletContext
+ renderer(req:HttpServletRequest).process(diagram, opt)
+ }
+
+ def renderer(req:HttpServletRequest) = new DiagramRenderer(wd(req:HttpServletRequest))
+
+ def wd(req:HttpServletRequest) = {
val here = new File(req.getSession.getServletContext.getRealPath("x")).getParentFile
if (!here.exists) throw new BadDiagram("Server error, here directory missing " + here.getAbsolutePath)
val workDir = new File(here.getParentFile, "cache")
if (!workDir.exists) throw new BadDiagram("Server error, work directory missing " + workDir.getAbsolutePath)
if (!workDir.isDirectory) throw new BadDiagram("Server error, check work directory " + workDir.getAbsolutePath)
- val renderer = new DiagramRenderer(workDir)
- renderer.process(diagram, req.getParameter("opt"))
+ workDir
}
override def doGet(req : HttpServletRequest, res : HttpServletResponse) : Unit = {
View
@@ -7,13 +7,13 @@
body {
margin: 0px;
- font-family: Arial,Helvetica,sans-serif;
+ font-family: 'Nunito', sans-serif;
}
#titlebar {
font-family: 'Nunito', sans-serif;
background: #B6D0DE;
- height: 64px;
+ height: 56px;
position: relative;
}
@@ -59,13 +59,13 @@ body {
height: 100%;
}
-div.historyEntry {
+div.diagramEntry {
margin-top: 1em;
margin-bottom: 1em;
text-align: center;
}
-div.historyEntry img {
+div.diagramEntry img {
cursor: pointer;
}
@@ -76,6 +76,22 @@ div.historyEntry img {
overflow: auto;2
}
+#samples0 {
+ position: absolute;
+ right: 132;
+ width: 131px;
+ background: #F2F2F2;
+ overflow: auto;2
+}
+
+#samples1 {
+ position: absolute;
+ right: 0;
+ width: 131px;
+ background: #F2F2F2;
+ overflow: auto;2
+}
+
#errorDiv {
position: absolute;
height: 100px;
Oops, something went wrong.

0 comments on commit a0647a6

Please sign in to comment.