Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.80 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Filtering Markdown content in Jade / Node.js
  2. html = function(req, res, next) {
  3.   if (!/.html$/.test(res.viewPath)) return next();
  4.   return fs.readFile(res.viewPath, "utf8", function(error, htmlText) {
  5.     res.html = htmlText;
  6.     return next(error);
  7.   });
  8. };
  9.        
  10. markdownToHTML = function(req, res, next) {
  11.   if (!/.md$/.test(res.viewPath)) return next();
  12.   return fs.readFile(res.viewPath, "utf8", function(error, markdownText) {
  13.     res.html = markdown(markdownText);
  14.     return next(error);
  15.   });
  16. };
  17.        
  18. blogArticle = function(req, res, next) {
  19.   var footerPath, post;
  20.   post = res.post;
  21.   footerPath = path.join(__dirname, "..", "templates", "blog_layout.jade");
  22.   return fs.readFile(footerPath, "utf8", function(error, jadeText) {
  23.     var footerFunc;
  24.     if (error) return next(error);
  25.     footerFunc = jade.compile(jadeText);
  26.     res.html = footerFunc({
  27.       post: post,
  28.       body: res.html
  29.     });
  30.     return next();
  31.   });
  32. };
  33.        
  34. exports.layout = function(req, res, next) {
  35.   var layoutPath;
  36.   layoutPath = path.join(__dirname, "..", "templates", "layout.jade");
  37.   return fs.readFile(layoutPath, "utf8", function(error, jadeText) {
  38.     var layoutFunc, locals;
  39.     layoutFunc = jade.compile(jadeText, {
  40.       filename: layoutPath
  41.     });
  42.     locals = {
  43.       config: config,
  44.       title: "",
  45.       body: res.html || ""
  46.     };
  47.     res.html = layoutFunc(locals);
  48.     return next(error);
  49.   });
  50. };
  51.        
  52. exports.domify = function(req, res, next) {
  53.   return jsdom.env(res.html, [jqueryPath], function(error, dom) {
  54.     if (error) return next(error);
  55.     res.dom = dom;
  56.     dom.toMarkup = function() {
  57.       this.window.$("script").last().remove();
  58.       return this.window.document.doctype + this.window.document.innerHTML;
  59.     };
  60.     return next(error);
  61.   });
  62. };
  63.        
  64. exports.flickr = function(req, res, next) {
  65.   var $ = res.dom.window.$;
  66.   $("flickrshow").each(function(index, elem) {
  67.     var $elem, URLs;
  68.     $elem = $(elem);
  69.     URLs = $elem.attr("href");
  70.     return $elem.replaceWith(flickrshowTemplate.replace(/{URLs}/g, URLs));
  71.   });
  72.   return next();
  73. };
  74.        
  75. exports.youtube = function(req, res, next) {
  76.   var $ = res.dom.window.$;
  77.   $("youtube").each(function(index, elem) {
  78.     var $elem, URL;
  79.     $elem = $(elem);
  80.     URL = $elem.attr("href");
  81.     return $elem.replaceWith(youtubeTemplate.replace(/{URL}/, URL));
  82.   });
  83.   return next();
  84. };
  85.        
  86. postTitle = function(req, res, next) {
  87.   var $;
  88.   $ = res.dom.window.$;
  89.   $("title").text(res.post.title + " | Peter Lyons");
  90.   return next();
  91. };
  92.        
  93. exports.undomify = function(req, res, next) {
  94.   res.html = res.dom.toMarkup();
  95.   return next();
  96. };
  97.        
  98. exports.send = function(req, res) {
  99.   return res.send(res.html);
  100. };
  101.        
  102. postMiddleware = [
  103.   loadPost,
  104.   html,
  105.   markdownToHTML,
  106.   blogArticle,
  107.   layout,
  108.   domify,
  109.   postTitle,
  110.   flickr,
  111.   youtube,
  112.   undomify,
  113.   send
  114. ]
  115.  
  116. app.get("/your/uri", postMiddleware);