Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
logs
project/project
project/target
target
tmp
.history
dist
4 changes: 4 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is your new Play 2.0 application
=====================================

This file will be packaged with your application, when using `play dist`.
51 changes: 51 additions & 0 deletions app/controllers/Application.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package controllers

import play.api._
import play.api.mvc._
import java.io.File
import paper.Analyze
import generators._

object Application extends Controller {

def index = Action {
Ok(views.html.index(""))
}

def generateSchedulePdf = Action { request =>
val body = request.body.asFormUrlEncoded
if(body != None) {
val papers = body.get("papers[]").toList
val abstractGet = body.get("abstract")

val abstractVal = if(abstractGet.length == 1) abstractGet.toList.head.toInt else 0

Ok(SchedulePdfGenerator.apply(papers, abstractVal))
} else Ok("Something went wrong")
}

def generateTaskProcess(task: String, path: String) = Action {
Ok(TaskProcessGenerator.apply(task, utf8URLDecode(path)))
}

def generatePersonalGraph(link: String) = Action {
Ok(views.html.index(link))
}

def generateLink = Action { request =>
val body = request.body.asFormUrlEncoded
if(body != None) {
val email = body.get("useremail").toList
}
// TODO
Redirect(routes.Application.index)
}

def getGraphData(link: String) = Action {
Ok("") // TODO
}

private def utf8URLDecode(url: String): String = {
return """/%u([0-9a-f]{3,4})/i""".r.replaceAllIn(java.net.URLDecoder.decode(url.replace("\\00", "%u00"), "UTF-8"), "&#x\\1;")
}
}
File renamed without changes.
214 changes: 214 additions & 0 deletions app/generators/SchedulePdfGenerator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package generators

import play.api._
import play.api.mvc._
import play.i18n.Lang
import play.api.libs.json._
import scala.io.Source
import java.io.File
import java.security.MessageDigest


case class Paper(id: String, title: String, authors: String, pdf: String, date: String, room: String) {
def getId: String = this.id
def getTitle: String = this.title
def getAuthors: String = this.authors
def getPdf: String = this.pdf
def getDate: String = this.date
def getRoom: String = this.room

def setId(newId: String): Paper = Paper(newId, title, authors, pdf, date, room)
def setTitle(newTitle: String): Paper = Paper(id, newTitle, authors, pdf, date, room)
def setAuthors(newAuthors: String): Paper = Paper(id, title, newAuthors, pdf, date, room)
def setPdf(newPdf: String): Paper = Paper(id, title, authors, newPdf, date, room)
def setDate(newDate: String): Paper = Paper(id, title, authors, pdf, newDate, room)
def setRoom(newRoom: String): Paper = Paper(id, title, authors, pdf, date, newRoom)
}

object PaperCreator {
def createPaper(json: JsValue): Paper = {
Paper((json \ "id").toString, (json \ "title").toString.drop(1).dropRight(1), (json \ "authors").toString.drop(1).dropRight(1), (json \ "pdf").toString.drop(1).dropRight(1), (json \ "date").toString.drop(1).dropRight(1), (json \ "room").toString.drop(1).dropRight(1))
}
}

object SchedulePdfGenerator {
def apply(selectedPapers: List[String], abstractVal: Int): String = {
val json: JsValue = Json.parse(Source.fromFile("public/js/data.json").mkString)
val nodes = (json \\ "nodes").head

// Dividing to get seconds in place of milliseconds
//date_default_timezone_set ( "UTC" );
val papers = changeDate(getPapers(nodes, selectedPapers))


// Get Temporary directory
val postfix = md5(Math.random.toInt.toString).substring(0,7)
val directory = "public/tex/temp/" + postfix
(new File(directory)).mkdir

val tempContent = createSchedule(abstractVal, papers, directory)
val tempOutput = directory + "/isit_schedule"

// Create system call for generating pdf
val call = "cat public/tex/header.tex " + tempContent + " public/tex/footer.tex | pdflatex --jobname " + tempOutput

/*
// Make system call
$ret = exec($call);

// Create temporary page "your download should appear in a moment. If not click here"
echo page_output($temp_output);

// Redirect to schedule
header( 'Location: '.$temp_output.'.pdf' ) ;
*/ ""
}

private def md5(s: String): String = {
var algorithm = MessageDigest.getInstance("MD5")
algorithm.update(s.getBytes)
algorithm.digest.toString
}

private def changeDate(papers: List[Paper]): List[Paper] = {
def changeDate0(papers: List[Paper], accu: List[Paper]): List[Paper] = papers match {
case List() => accu
case x::xs => changeDate0(xs, x.setDate(x.getDate.dropRight(3)) :: accu)
}

changeDate0(papers, List()).reverse
}

/**
* Creates the content part of the schedule file
*/
private def createSchedule(abstractVal: Int, papers: List[Paper], directory: String): String = {

// Sorts papers by date
//uasort($papers, "sort_by_date");

// Schedule and date
var schedule = ""
var date = ""
var time = ""
var first = true

// Now for each paper, write it out
papers.foreach (p => {
// Check if the date is the same as before
val newDate = ""//date('l \t\h\e jS \of F Y', p.getDate)
val newTime = ""//date('h:i A', p.getDate)

// Add day
if (!newDate.equals(date)) {
if (!first) schedule = schedule + endList

schedule = schedule + addScheduleDay(newDate) + startList
first = false
}

// Add schedule point
schedule = if (newTime.equals(time)) schedule + addSchedulePoint(abstractVal, p, "")
else schedule + addSchedulePoint(abstractVal, p, newTime)

// Add schedule point and update date
date = newDate
time = newTime
}
)

// Save schedule to file
val file = directory + "/content.tex"
val fileObj = new File(file)
fileObj.createNewFile
val p = new java.io.PrintWriter(fileObj)

p.print(schedule)
p.close

file
}

/**
* Constructs the latex code for a single paper in the schedule
*/
private def addSchedulePoint(abstractVal:Int, p: Paper, time: String): String = {

// Get abstract
var abstr = ""
if (abstractVal == 1) {
abstr = TaskProcessGenerator.apply("abstract", p.getPdf)
abstr = latexSpecialChars(abstr)
abstr = "{\\small " + abstr + "} \n"
}

// Get title
val title = "{\\it " + latexSpecialChars( p.getTitle/*, "\\'\"&\n\r{}[]"*/ ) + "} \\\\ \n";

// Get time and place
val point = if (time.equals("")) "\\item[{\\hfill " + p.getRoom + "}]\n"
else "\\item[{\\hfill \bf " + time + "} \\\\ {\\hfill " + p.getRoom + "}]\n"

// Get authors
val authors = "{" + p.getAuthors + "} \\\\ \n"

point + title + authors + abstr + "\n"
}

private def startList: String = {
"%\n%\n\\begin{enumerate}[leftmargin=5cm, labelsep=0.3cm, rightmargin=2cm, align=right, itemsep=1cm, style=multiline]\n%\n"
}

private def endList: String = {
"\\end{enumerate}\n%\n"
}

/**
* Constructs the latex code for a page dedicated to a certain day
*/
private def addScheduleDay(date: String) {
"\\clearpage\\period{" + date + "}\\hfil\\break \\\\ \n"
}

/**
* Sorts two items based on their date
*/
private def sortByDate(a: Paper, b: Paper): Int = {
return if(a.getDate < b.getDate) -1 else 1
}


/**
* Displays a field for downloading the schedule
*/
private def pageOutput(tempOutput: String): String = {
"<head><title>TrailHead</title></head><body><p>If your download didn't start automatically, click <a href=\"" + tempOutput + ".pdf\" title=\"Schedule as PDF\">here</a></p></body>"
}

private def latexSpecialChars( str: String ): String = {
val map = Map(
"#"->"\\#",
"$"->"\\$",
"%"->"\\%",
"&"->"\\&",
"~"->"\\~{}",
"_"->"\\_",
"^"->"\\^{}",
"\\"->"\\textbackslash",
"{"->"\\{",
"}"->"\\}"
)
return """([\^\%~\\\\#\$%&_\{\}])""".r.replaceAllIn(str, m => map(m.group(1)))
}

private def getPapers(array: JsValue, selectedPapers: List[String]): List[Paper] = {
var output: List[Paper] = List[Paper]()

selectedPapers.foreach((id: String) => {
val paper = PaperCreator.createPaper(array.apply(id.toInt))
output = paper.setId(id) :: output
})

output.reverse
}
}
46 changes: 46 additions & 0 deletions app/generators/TaskProcessGenerator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package generators

import play.api._
import play.api.mvc._
import play.i18n.Lang
import scala.io.Source
import java.io.File
import paper._

object TaskProcessGenerator {
def apply(task: String, path: String): String = {
return if(task.equals("abstract")) getAbstract(path) else "Task not recognized"
}

private def getAbstract(pdf: String): String = {
val array = List[(String, String)](("&ucirc;", "fi"), ("&quot;d", "&le;"), ("&iuml;&not;", "fi"), ("&acirc;‰&curren;", "&le;"), ("&ordm;", "&#954;"),
("&acute;", "&#948;"), ("&quot;&yen;", "&#8869;"), ("&quot;H", "&#8776;"), ("&quot;", "&#9827;"), ("&sup3;", "&#947;"), ("&Aacute;", "&#961;"),
("&Auml;", "&#964;"), ("&raquo;", "&#955;"))


val files = Analyze.main(Array[String]("public/" + pdf, "-p"))

if(files.isEmpty) return "Can't load the abstract"
else return replaceWithList(files.head.getAbstract.getText, array)
}

private def replaceWithList(str: String, array: List[(String, String)]): String = {
var output = str

array.foreach((l: (String, String)) => output = output.replace(l._1, l._2))

output
}

private def repeatStr(str: String, nb: Int): String = {
var output = ""
var i = 0

while(i < nb) {
output = output + str
i = i + 1
}

output
}
}
64 changes: 64 additions & 0 deletions app/paper/Analyze.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package paper

object Analyze {
def main(args : Array[String]): List[Paper] = {
// create analyzer
val A : Analyzer = new Analyzer()

// Check that a directory is supplied (there is an argument)
if (args.length == 0 || args.length > 2) {println("You should provide at leath a path and at most a path and an option. Type -h for help.");List()}

else if(args.contains("-h")) { println("How to call: Analyze [path] [parameter]?\nPARAMETERS:\n\t-p : parsing\n\t-s : looks for xml scheduler\n\t-c : compare\n\t-e : extend\n\t-g : create graph\n\t-h : shows this help page\n\tnothing : do everything"); List()}
// Then go ahead
else A.analyze(args(0), args.toList.tail)
}
}

class Analyzer extends Object with LoadPaper
with ParsePaper
with ExtendPaper
with ComparePaper
with XMLScheduleParser
with Graphs {

// Set a limit in percent for when papers get an edge between them
val limit : Int = 1

// Get cached papers in this order
val cache : List[String] = List(Cache.linked, Cache.extended, Cache.scheduled, Cache.parsed)

// Set sources we want to extend with
//val sources : List[PaperSource] = List(TalkDates, TalkRooms, PdfLink)
val sources : List[PaperSource] = List(PdfLink)

// Analyze a paper
def analyze(paperPos: String, options: List[String]): List[Paper] = {
// Get a list of parsed papers
val papers : Option[List[Paper]] = if(options.isEmpty || options.contains("-p")) Some(loadAndParse(paperPos, cache, XMLParser, XMLConverterLoader)) else None

// Mix in the schedule XML data
val xmlPapers : Option[List[Paper]] = if(options.isEmpty || options.contains("-s")) Some(getXMLSchedule(paperPos, papers)) else None

// Extend papers with tertiary data
val extendedPapers : Option[List[Paper]] = if(options.isEmpty || options.contains("-e")) Some(extend(paperPos, xmlPapers, sources)) else None

// Compare the papers individually
val comparedPapers : Option[List[Paper]] = if(options.isEmpty || options.contains("-c")) Some(compare(paperPos, extendedPapers, limit)) else None


if(options.isEmpty || options.contains("-g")){
// Create graph
val graph : Graph = getGraph(paperPos, comparedPapers)

// Print graph to file 'data.json'
graph.save
}

if(options.contains("-p")) return papers.get
else if(options.contains("-s")) return xmlPapers.get
else if(options.contains("-e")) return extendedPapers.get
else if(options.isEmpty || options.contains("-c")) return comparedPapers.get

List()
}
}
Loading