Skip to content

Instantly share code, notes, and snippets.

@dpp
Created November 8, 2012 18:31
Show Gist options
  • Save dpp/4040614 to your computer and use it in GitHub Desktop.
Save dpp/4040614 to your computer and use it in GitHub Desktop.
Telegram/Hoisted Plugin
package sloth
import org.hoisted.lib._
import net.liftweb._
import common._
import util._
import Helpers._
import scala.xml._
object Moose {
def renderNews(xml: NodeSeq): NodeSeq => NodeSeq = {
def order(n: Node): Int = (n \ "LinkID").headOption.flatMap(x => Helpers.asInt(x.text)) getOrElse 1
val what = (xml \ "Link").toList.sortWith((n, n2) => order(n) < order(n2))
def start = "http://www.lafayettedolphins.net/index.php?item="
def fixLink(in: Node): String = in.text match {
case s if s.startsWith(start) => "/content/"+s.substring(start.length).toLowerCase
case s if s.startsWith("http:") || s.startsWith("https:") => s
case s if !s.startsWith("/") => "/content/"+s.toLowerCase
case s => s
}
"li" #> what.map(item =>
"img [src+]" #> (((item \ "LinkType").headOption.map(_.text) match {
case Some("pdf") => "pdf_icon.png"
case _ => "external_link_icon.png"
}): String) andThen "a [href]" #> (item \ "LinkUrl").headOption.map(fixLink(_)) &
"a [target]" #> ((item \ "LinkType").headOption.map(_.text) match {
case Some("pdf") => "pdfWindow"
case Some("external") => "_blank"
case _ => "_self"
}) & "a -*" #> (item \ "LinkDisplayName").headOption.map(_.text))
}
}
object Item {
def fromXml(in: NodeSeq): List[Item] = {
((in \ "Item").toList.flatMap(fromXml(_)) :::
(in \ "SubItem").toList.flatMap(fromXml(_)) :::
(((in \ "ItemID").map(_.text).flatMap(Helpers.asInt(_)).headOption,
(in \ "ItemName").map(_.text.replace(" ", "_").toLowerCase).headOption,
(in \ "ItemDisplayName").map(_.text).headOption,
(in \ "ItemDescription").map(_.text).headOption) match {
case (Some(id), Some(name), Some(displayName), Some(description)) =>
List(Item(id, name, displayName, description, fromXml(in \ "SubItems")))
case _ => Nil
}) :::
(((in \ "SubItemID").map(_.text).flatMap(Helpers.asInt(_)).headOption,
(in \ "SubItemName").map(_.text.replace(" ", "_").toLowerCase).headOption,
(in \ "SubItemDisplayName").map(_.text).headOption,
(in \ "SubItemDescription").map(_.text).headOption) match {
case (Some(id), Some(name), Some(displayName), Some(description)) =>
List(Item(id, name, displayName, description, fromXml(in \ "SubItems")))
case _ => Nil
})).sortWith((a, b) => a.id < b.id)
}
}
case class Item(id: Int, name: String, displayName: String, description: String,
kids: List[Item]) {
def path = name match {
case "home" => "/home"
case x => "/content/"+x
}
}
class Moose extends PluginPhase1 {
def apply(in: List[ParsedFile]): List[ParsedFile] = {
val env = HoistedEnvironmentManager.value
val promo = in.filter(p => p.fileInfo.pathAndSuffix.path.startsWith("promotion_slide_show" :: Nil) &&
p.fileInfo.pathAndSuffix.suffix == Some("jpg"))
import Helpers._
env.addSnippet{case ("promo", "render") =>
Full("img" #> promo.map(file => "img [src]" #> file.fileInfo.pathAndSuffix.display))}
for {
xml <- HoistedUtil.xmlForFile("school_news" :: "links.xml" :: Nil,
in)
} {
env.addSnippet(
Map(("dolphins", "news") -> Full(Moose.renderNews(xml))))
}
for {
xml <- HoistedUtil.xmlForFile("navigation" :: "navigation.xml" :: Nil, in)
} {
def currentPath(i: Item): Boolean = {
CurrentFile.value match {
case null => false
case f => f.fileInfo.pathAndSuffix.path.contains(i.name)
}
}
val items = Item.fromXml(xml \ "Items")
env.addSnippet{
case ("dmenu", "short") => Full("li" #> items.map(i => (if (currentPath(i)) "li [class+]" #> "selected" else PassThru) andThen
"a *" #> i.displayName & "a [href]" #> i.path))
case ("dmenu", "bottom") => Full("li" #> items.zipWithIndex.map{
case (i, _) if i.kids.isEmpty =>
"a [class]" #> "header" & "a [href]" #> i.path & "a *" #> i.displayName
case (i, pos) =>
//echo "<a id=\"footer_".$ItemCount."\" class=\"header\" href=\"" . $itemName . "/index.html\" onMouseover=\"showmenu('footer_".$ItemCount."',event,linkset[ ".$ItemCount."])\" onMouseout=\"delayhidemenu()\">" . $item->ItemDisplayName . "</a>";
"a [id]" #> ("footer_"+(pos + 1)) & "a [class]" #> "header" & "a [href]" #> i.path &
"a [onMouseover]" #> ("showmenu('footer_"+(pos + 1)+"', event, linkset["+(pos + 1)+"])") & "a [onMouseout]" #> "delayhidemenu()" &
"a *" #> i.displayName
})
case ("dmenu", "left") =>
def topItem = items.find(i => CurrentFile.value.fileInfo.pathAndSuffix.path.contains(i.name)).headOption
Full("li" #> topItem.toList.flatMap(topper => {
topper.kids.map(i => (if (currentPath(i)) "li [class+]" #> "selected" else PassThru) andThen "a *" #> i.displayName & "a [href]" #>
(if (i.name == "home") "/home" else "/content/"+topper.name+"/"+i.name))}))
}
}
in.flatMap(pf => pf.fileInfo.pathAndSuffix.path match {
case "content" :: rest if rest.takeRight(1) != List("index") =>
val fi = pf.fileInfo
val ps = fi.pathAndSuffix
val nps = ps.copy(path = ps.path.dropRight(1) ::: List("index"))
val nfi = nps.toFileInfo(Empty)
val ret = pf.updateFileInfo(nfi)
List(pf, ret)
case _ => List(pf)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment