Skip to content

Commit

Permalink
Moved samples to the main page. Simplified things.
Browse files Browse the repository at this point in the history
Probably should start using lift actually. Not right now though.
  • Loading branch information
vpatryshev committed Oct 15, 2011
1 parent 6847cb0 commit a0647a6
Show file tree
Hide file tree
Showing 13 changed files with 434 additions and 185 deletions.
8 changes: 8 additions & 0 deletions .idea/highlighting.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/main/scala/cat2xy/Layouts.scala
@@ -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)
}
}
62 changes: 0 additions & 62 deletions src/main/scala/web/DiagramPage.scala

This file was deleted.

10 changes: 5 additions & 5 deletions src/main/scala/web/DiagramSamples.scala
Expand Up @@ -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>
Expand All @@ -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>

Expand All @@ -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 {
Expand Down
120 changes: 49 additions & 71 deletions src/main/scala/web/DiagramService.scala
Expand Up @@ -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 => {
Expand All @@ -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)
}
*/
}
1 change: 0 additions & 1 deletion src/main/scala/web/HtmlSnippets.scala
Expand Up @@ -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 = {
Expand Down
15 changes: 12 additions & 3 deletions src/main/scala/web/PresheafServlet.scala
Expand Up @@ -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 = {
Expand Down
24 changes: 20 additions & 4 deletions web/1.css
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down

0 comments on commit a0647a6

Please sign in to comment.