Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Replacing github syntax highlighted code blocks with showdown friendly pre tags #10

Merged
merged 1 commit into from over 2 years ago

2 participants

Damon Oehlman Rodrigo Thauby
Damon Oehlman

Firstly - thanks for Paige, exactly what I was after :)

One thing I did have in my README files though was a lot of the github flavoured syntax highlighting which was not getting handled well by the showdown processor. So I popped a regex to replace those with pre tags prior to processing.

I don't usually code in coffeescript though, so it's probably worth checking that no brackets slipped in ;)

Rodrigo Thauby rthauby merged commit c3108dd into from January 01, 2012
Rodrigo Thauby rthauby closed this January 01, 2012
Rodrigo Thauby
Owner

Hey, nice work on this. I'll be pushing a new version with a few minor tweaks (including this one) to npm today. Thanks for the feature!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 29, 2011
Damon Oehlman Added regex to replace ``` code blocks with <pre> tags 05de6a8
This page is out of date. Refresh to see the latest.
66  lib/paige.js
... ...
@@ -1,14 +1,23 @@
1 1
 (function() {
2  
-  var check_for_rocco, clean_file_extension, clean_path_names, config_template, configuration, copy_image, ensure_directory, events, exec, fs, get_subfiles, mdown_template, path, process_config, process_html_file, read_config, rocco, showdown, spawn, subfiles, template, _, _ref;
3  
-  var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  2
+  var check_for_rocco, clean_file_extension, clean_path_names, config_template, configuration, copy_image, ensure_directory, events, exec, fs, get_subfiles, mdown_template, path, process_config, process_html_file, read_config, rocco, showdown, spawn, subfiles, template, _, _ref,
  3
+    _this = this;
  4
+
4 5
   _ = require("underscore");
  6
+
5 7
   fs = require('fs');
  8
+
6 9
   path = require('path');
  10
+
7 11
   showdown = require('./../vendor/showdown').Showdown;
  12
+
8 13
   _ref = require('child_process'), spawn = _ref.spawn, exec = _ref.exec;
  14
+
9 15
   events = require('events');
  16
+
10 17
   rocco = require('./rocco.js');
  18
+
11 19
   subfiles = [];
  20
+
12 21
   configuration = {
13 22
     "title": "Untitled",
14 23
     "content_file": "README.mdown",
@@ -19,12 +28,11 @@
19 28
     "background": "bright_squares",
20 29
     "output": "docs"
21 30
   };
  31
+
22 32
   read_config = function(callback) {
23 33
     var filename;
24 34
     filename = "paige.config";
25  
-    if (process.ARGV[2] != null) {
26  
-      filename = process.ARGV[2];
27  
-    }
  35
+    if (process.argv[2] != null) filename = process.argv[2];
28 36
     return fs.readFile(filename, "utf-8", function(error, data) {
29 37
       var config;
30 38
       if (error) {
@@ -34,47 +42,44 @@
34 42
       } else {
35 43
         config = JSON.parse(data);
36 44
         process_config(config);
37  
-        if (callback) {
38  
-          return callback(config);
39  
-        }
  45
+        if (callback) return callback(config);
40 46
       }
41 47
     });
42 48
   };
  49
+
43 50
   process_config = function(config) {
44  
-    if (config == null) {
45  
-      config = {};
46  
-    }
  51
+    if (config == null) config = {};
47 52
     return _.map(config, function(value, key, list) {
48  
-      if (config[key] != null) {
49  
-        return configuration[key] = value;
50  
-      }
  53
+      if (config[key] != null) return configuration[key] = value;
51 54
     });
52 55
   };
  56
+
53 57
   ensure_directory = function(dir, callback) {
54 58
     return exec("mkdir -p " + dir, function() {
55 59
       return callback();
56 60
     });
57 61
   };
  62
+
58 63
   copy_image = function() {
59 64
     var desired_image;
60 65
     desired_image = fs.readFileSync(__dirname + ("/../resources/" + configuration.background + ".png"));
61 66
     return fs.writeFile("" + configuration.output + "/bg.png", desired_image);
62 67
   };
  68
+
63 69
   process_html_file = function() {
64 70
     var clean_subfiles, source, subfiles_names;
65 71
     source = configuration.content_file;
66 72
     if (configuration.include_index) {
67 73
       subfiles_names = clean_file_extension(subfiles);
68 74
     }
69  
-    if (configuration.include_index) {
70  
-      clean_subfiles = clean_path_names(subfiles);
71  
-    }
  75
+    if (configuration.include_index) clean_subfiles = clean_path_names(subfiles);
72 76
     return fs.readFile(source, "utf-8", function(error, code) {
73 77
       var content_html, html;
74 78
       if (error) {
75 79
         console.log("\nThere was a problem reading your the content file: " + source);
76 80
         throw error;
77 81
       } else {
  82
+        code = code.replace(/```\w*\n?([^```]*)```/gm, '<pre>\n$1</pre>');
78 83
         content_html = showdown.makeHtml(code);
79 84
         html = mdown_template({
80 85
           content_html: content_html,
@@ -90,26 +95,24 @@
90 95
       }
91 96
     });
92 97
   };
  98
+
93 99
   template = function(str) {
94 100
     return new Function('obj', 'var p=[],print=function(){p.push.apply(p,arguments);};' + 'with(obj){p.push(\'' + str.replace(/[\r\t\n]/g, " ").replace(/'(?=[^<]*%>)/g, "\t").split("'").join("\\'").split("\t").join("'").replace(/<%=(.+?)%>/g, "',$1,'").split('<%').join("');").split('%>').join("p.push('") + "');}return p.join('');");
95 101
   };
96  
-  get_subfiles = __bind(function(callback) {
  102
+
  103
+  get_subfiles = function(callback) {
97 104
     var count, find_files;
98 105
     count = 0;
99  
-    find_files = __bind(function(file, total) {
  106
+    find_files = function(file, total) {
100 107
       var f_file, f_path;
101 108
       f_path = file.substr(0, file.lastIndexOf('/') + 1);
102 109
       f_file = file.substr(file.lastIndexOf('/') + 1);
103 110
       return exec("find ./" + f_path + " -name '" + f_file + "' -print", function(error, stdout, stderr) {
104 111
         count++;
105 112
         subfiles = _.uniq(_.union(subfiles, stdout.trim().split("\n")));
106  
-        if (count >= total) {
107  
-          if (callback) {
108  
-            return callback();
109  
-          }
110  
-        }
  113
+        if (count >= total) if (callback) return callback();
111 114
       });
112  
-    }, this);
  115
+    };
113 116
     if (_.isArray(configuration.docco_files)) {
114 117
       return _.each(configuration.docco_files, function(file) {
115 118
         return find_files(file, configuration.docco_files.length);
@@ -117,7 +120,8 @@
117 120
     } else if (_.isString(configuration.docco_files)) {
118 121
       return find_files(configuration.docco_files, 1);
119 122
     }
120  
-  }, this);
  123
+  };
  124
+
121 125
   clean_path_names = function(names) {
122 126
     var clean_names;
123 127
     clean_names = [];
@@ -126,6 +130,7 @@
126 130
     });
127 131
     return clean_names;
128 132
   };
  133
+
129 134
   clean_file_extension = function(names) {
130 135
     var clean_names;
131 136
     clean_names = [];
@@ -134,13 +139,15 @@
134 139
     });
135 140
     return clean_names;
136 141
   };
  142
+
137 143
   check_for_rocco = function() {
138  
-    if (configuration.docco_files != null) {
139  
-      return rocco(subfiles, configuration);
140  
-    }
  144
+    if (configuration.docco_files != null) return rocco(subfiles, configuration);
141 145
   };
  146
+
142 147
   mdown_template = template(fs.readFileSync(__dirname + '/../resources/paige.jst').toString());
  148
+
143 149
   config_template = fs.readFileSync(__dirname + '/../resources/paige.config').toString();
  150
+
144 151
   read_config(function(config) {
145 152
     return ensure_directory(configuration.output, function() {
146 153
       return get_subfiles(function() {
@@ -150,4 +157,5 @@
150 157
       });
151 158
     });
152 159
   });
  160
+
153 161
 }).call(this);
3  src/paige.coffee
@@ -66,6 +66,9 @@ process_html_file = ->
66 66
       console.log "\nThere was a problem reading your the content file: #{source}"
67 67
       throw error
68 68
     else
  69
+      # replace ``` block with <pre> tags
  70
+      code = code.replace /```\w*\n?([^```]*)```/gm, '<pre>\n$1</pre>'
  71
+    
69 72
       content_html = showdown.makeHtml code
70 73
       html = mdown_template {
71 74
         content_html:     content_html,
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.