Gulpfile.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. "use strict";
  2. var os = require("os");
  3. var gulp = require("gulp");
  4. var gutil = require("gulp-util");
  5. var sass = require("gulp-ruby-sass");
  6. var jshint = require("gulp-jshint");
  7. var uglify = require("gulp-uglifyjs");
  8. var rename = require("gulp-rename");
  9. var concat = require("gulp-concat");
  10. var notify = require("gulp-notify");
  11. var header = require("gulp-header");
  12. var minifycss = require("gulp-minify-css");
  13. //var jsdoc = require("gulp-jsdoc");
  14. //var jsdoc2md = require("gulp-jsdoc-to-markdown");
  15. var pkg = require("./package.json");
  16. var dateFormat = require("dateformatter").format;
  17. var replace = require("gulp-replace");
  18. pkg.name = "Editor.md";
  19. pkg.today = dateFormat;
  20. var headerComment = ["/*",
  21. " * <%= pkg.name %>",
  22. " *",
  23. " * @file <%= fileName(file) %> ",
  24. " * @version v<%= pkg.version %> ",
  25. " * @description <%= pkg.description %>",
  26. " * @license MIT License",
  27. " * @author <%= pkg.author %>",
  28. " * {@link <%= pkg.homepage %>}",
  29. " * @updateTime <%= pkg.today('Y-m-d') %>",
  30. " */",
  31. "\r\n"].join("\r\n");
  32. var headerMiniComment = "/*! <%= pkg.name %> v<%= pkg.version %> | <%= fileName(file) %> | <%= pkg.description %> | MIT License | By: <%= pkg.author %> | <%= pkg.homepage %> | <%=pkg.today('Y-m-d') %> */\r\n";
  33. var scssTask = function(fileName, path) {
  34. path = path || "scss/";
  35. var distPath = "css";
  36. return sass(path + fileName + ".scss", { style: "expanded", sourcemap: false, noCache : true })
  37. .pipe(gulp.dest(distPath))
  38. .pipe(header(headerComment, {pkg : pkg, fileName : function(file) {
  39. var name = file.path.split(file.base);
  40. return name[1].replace("\\", "");
  41. }}))
  42. .pipe(gulp.dest(distPath))
  43. .pipe(rename({ suffix: ".min" }))
  44. .pipe(gulp.dest(distPath))
  45. .pipe(minifycss())
  46. .pipe(gulp.dest(distPath))
  47. .pipe(header(headerMiniComment, {pkg : pkg, fileName : function(file) {
  48. var name = file.path.split(file.base);
  49. return name[1].replace("\\", "");
  50. }}))
  51. .pipe(gulp.dest(distPath))
  52. .pipe(notify({ message: fileName + ".scss task completed!" }));
  53. };
  54. gulp.task("scss", function() {
  55. return scssTask("editormd");
  56. });
  57. gulp.task("scss2", function() {
  58. return scssTask("editormd.preview");
  59. });
  60. gulp.task("scss3", function() {
  61. return scssTask("editormd.logo");
  62. });
  63. gulp.task("js", function() {
  64. return gulp.src("./src/editormd.js")
  65. .pipe(jshint("./.jshintrc"))
  66. .pipe(jshint.reporter("default"))
  67. .pipe(header(headerComment, {pkg : pkg, fileName : function(file) {
  68. var name = file.path.split(file.base);
  69. return name[1].replace(/[\\\/]?/, "");
  70. }}))
  71. .pipe(gulp.dest("./"))
  72. .pipe(rename({ suffix: ".min" }))
  73. .pipe(uglify()) // {outSourceMap: true, sourceRoot: './'}
  74. .pipe(gulp.dest("./"))
  75. .pipe(header(headerMiniComment, {pkg : pkg, fileName : function(file) {
  76. var name = file.path.split(file.base + ( (os.platform() === "win32") ? "\\" : "/") );
  77. return name[1].replace(/[\\\/]?/, "");
  78. }}))
  79. .pipe(gulp.dest("./"))
  80. .pipe(notify({ message: "editormd.js task complete" }));
  81. });
  82. gulp.task("amd", function() {
  83. var replaceText1 = [
  84. 'var cmModePath = "codemirror/mode/";',
  85. ' var cmAddonPath = "codemirror/addon/";',
  86. '',
  87. ' var codeMirrorModules = [',
  88. ' "jquery", "marked", "prettify",',
  89. ' "katex", "raphael", "underscore", "flowchart", "jqueryflowchart", "sequenceDiagram",',
  90. '',
  91. ' "codemirror/lib/codemirror",',
  92. ' cmModePath + "css/css",',
  93. ' cmModePath + "sass/sass",',
  94. ' cmModePath + "shell/shell",',
  95. ' cmModePath + "sql/sql",',
  96. ' cmModePath + "clike/clike",',
  97. ' cmModePath + "php/php",',
  98. ' cmModePath + "xml/xml",',
  99. ' cmModePath + "markdown/markdown",',
  100. ' cmModePath + "javascript/javascript",',
  101. ' cmModePath + "htmlmixed/htmlmixed",',
  102. ' cmModePath + "gfm/gfm",',
  103. ' cmModePath + "http/http",',
  104. ' cmModePath + "go/go",',
  105. ' cmModePath + "dart/dart",',
  106. ' cmModePath + "coffeescript/coffeescript",',
  107. ' cmModePath + "nginx/nginx",',
  108. ' cmModePath + "python/python",',
  109. ' cmModePath + "perl/perl",',
  110. ' cmModePath + "lua/lua",',
  111. ' cmModePath + "r/r", ',
  112. ' cmModePath + "ruby/ruby", ',
  113. ' cmModePath + "rst/rst",',
  114. ' cmModePath + "smartymixed/smartymixed",',
  115. ' cmModePath + "vb/vb",',
  116. ' cmModePath + "vbscript/vbscript",',
  117. ' cmModePath + "velocity/velocity",',
  118. ' cmModePath + "xquery/xquery",',
  119. ' cmModePath + "yaml/yaml",',
  120. ' cmModePath + "erlang/erlang",',
  121. ' cmModePath + "jade/jade",',
  122. '',
  123. ' cmAddonPath + "edit/trailingspace", ',
  124. ' cmAddonPath + "dialog/dialog", ',
  125. ' cmAddonPath + "search/searchcursor", ',
  126. ' cmAddonPath + "search/search", ',
  127. ' cmAddonPath + "scroll/annotatescrollbar", ',
  128. ' cmAddonPath + "search/matchesonscrollbar", ',
  129. ' cmAddonPath + "display/placeholder", ',
  130. ' cmAddonPath + "edit/closetag", ',
  131. ' cmAddonPath + "fold/foldcode",',
  132. ' cmAddonPath + "fold/foldgutter",',
  133. ' cmAddonPath + "fold/indent-fold",',
  134. ' cmAddonPath + "fold/brace-fold",',
  135. ' cmAddonPath + "fold/xml-fold", ',
  136. ' cmAddonPath + "fold/markdown-fold",',
  137. ' cmAddonPath + "fold/comment-fold", ',
  138. ' cmAddonPath + "mode/overlay", ',
  139. ' cmAddonPath + "selection/active-line", ',
  140. ' cmAddonPath + "edit/closebrackets", ',
  141. ' cmAddonPath + "display/fullscreen",',
  142. ' cmAddonPath + "search/match-highlighter"',
  143. ' ];',
  144. '',
  145. ' define(codeMirrorModules, factory);'
  146. ].join("\r\n");
  147. var replaceText2 = [
  148. "if (typeof define == \"function\" && define.amd) {",
  149. " $ = arguments[0];",
  150. " marked = arguments[1];",
  151. " prettify = arguments[2];",
  152. " katex = arguments[3];",
  153. " Raphael = arguments[4];",
  154. " _ = arguments[5];",
  155. " flowchart = arguments[6];",
  156. " CodeMirror = arguments[9];",
  157. " }"
  158. ].join("\r\n");
  159. gulp.src("src/editormd.js")
  160. .pipe(rename({ suffix: ".amd" }))
  161. .pipe(gulp.dest('./'))
  162. .pipe(header(headerComment, {pkg : pkg, fileName : function(file) {
  163. var name = file.path.split(file.base);
  164. return name[1].replace(/[\\\/]?/, "");
  165. }}))
  166. .pipe(gulp.dest("./"))
  167. .pipe(replace("/* Require.js define replace */", replaceText1))
  168. .pipe(gulp.dest('./'))
  169. .pipe(replace("/* Require.js assignment replace */", replaceText2))
  170. .pipe(gulp.dest('./'))
  171. .pipe(rename({ suffix: ".min" }))
  172. .pipe(uglify()) //{outSourceMap: true, sourceRoot: './'}
  173. .pipe(gulp.dest("./"))
  174. .pipe(header(headerMiniComment, {pkg : pkg, fileName : function(file) {
  175. var name = file.path.split(file.base + ( (os.platform() === "win32") ? "\\" : "/") );
  176. return name[1].replace(/[\\\/]?/, "");
  177. }}))
  178. .pipe(gulp.dest("./"))
  179. .pipe(notify({ message: "amd version task complete"}));
  180. });
  181. var codeMirror = {
  182. path : {
  183. src : {
  184. mode : "lib/codemirror/mode",
  185. addon : "lib/codemirror/addon"
  186. },
  187. dist : "lib/codemirror"
  188. },
  189. modes : [
  190. "css",
  191. "sass",
  192. "shell",
  193. "sql",
  194. "clike",
  195. "php",
  196. "xml",
  197. "markdown",
  198. "javascript",
  199. "htmlmixed",
  200. "gfm",
  201. "http",
  202. "go",
  203. "dart",
  204. "coffeescript",
  205. "nginx",
  206. "python",
  207. "perl",
  208. "lua",
  209. "r",
  210. "ruby",
  211. "rst",
  212. "smartymixed",
  213. "vb",
  214. "vbscript",
  215. "velocity",
  216. "xquery",
  217. "yaml",
  218. "erlang",
  219. "jade",
  220. ],
  221. addons : [
  222. "edit/trailingspace",
  223. "dialog/dialog",
  224. "search/searchcursor",
  225. "search/search",
  226. "scroll/annotatescrollbar",
  227. "search/matchesonscrollbar",
  228. "display/placeholder",
  229. "edit/closetag",
  230. "fold/foldcode",
  231. "fold/foldgutter",
  232. "fold/indent-fold",
  233. "fold/brace-fold",
  234. "fold/xml-fold",
  235. "fold/markdown-fold",
  236. "fold/comment-fold",
  237. "mode/overlay",
  238. "selection/active-line",
  239. "edit/closebrackets",
  240. "display/fullscreen",
  241. "search/match-highlighter"
  242. ]
  243. };
  244. gulp.task("cm-mode", function() {
  245. var modes = [
  246. codeMirror.path.src.mode + "/meta.js"
  247. ];
  248. for(var i in codeMirror.modes) {
  249. var mode = codeMirror.modes[i];
  250. modes.push(codeMirror.path.src.mode + "/" + mode + "/" + mode + ".js");
  251. }
  252. return gulp.src(modes)
  253. .pipe(concat("modes.min.js"))
  254. .pipe(gulp.dest(codeMirror.path.dist))
  255. .pipe(uglify()) // {outSourceMap: true, sourceRoot: codeMirror.path.dist}
  256. .pipe(gulp.dest(codeMirror.path.dist))
  257. .pipe(header(headerMiniComment, {pkg : pkg, fileName : function(file) {
  258. var name = file.path.split(file.base + "\\");
  259. return (name[1]?name[1]:name[0]).replace(/\\/g, "");
  260. }}))
  261. .pipe(gulp.dest(codeMirror.path.dist))
  262. .pipe(notify({ message: "codemirror-mode task complete!" }));
  263. });
  264. gulp.task("cm-addon", function() {
  265. var addons = [];
  266. for(var i in codeMirror.addons) {
  267. var addon = codeMirror.addons[i];
  268. addons.push(codeMirror.path.src.addon + "/" + addon + ".js");
  269. }
  270. return gulp.src(addons)
  271. .pipe(concat("addons.min.js"))
  272. .pipe(gulp.dest(codeMirror.path.dist))
  273. .pipe(uglify()) //{outSourceMap: true, sourceRoot: codeMirror.path.dist}
  274. .pipe(gulp.dest(codeMirror.path.dist))
  275. .pipe(header(headerMiniComment, {pkg : pkg, fileName : function(file) {
  276. var name = file.path.split(file.base + "\\");
  277. return (name[1]?name[1]:name[0]).replace(/\\/g, "");
  278. }}))
  279. .pipe(gulp.dest(codeMirror.path.dist))
  280. .pipe(notify({ message: "codemirror-addon.js task complete" }));
  281. });
  282. /*
  283. gulp.task("jsdoc", function(){
  284. return gulp.src(["./src/editormd.js", "README.md"])
  285. .pipe(jsdoc.parser())
  286. .pipe(jsdoc.generator("./docs/html"));
  287. });
  288. gulp.task("jsdoc2md", function() {
  289. return gulp.src("src/js/editormd.js")
  290. .pipe(jsdoc2md())
  291. .on("error", function(err){
  292. gutil.log(gutil.colors.red("jsdoc2md failed"), err.message);
  293. })
  294. .pipe(rename(function(path) {
  295. path.extname = ".md";
  296. }))
  297. .pipe(gulp.dest("docs/markdown"));
  298. });
  299. */
  300. gulp.task("watch", function() {
  301. gulp.watch("scss/editormd.scss", ["scss"]);
  302. gulp.watch("scss/editormd.preview.scss", ["scss", "scss2"]);
  303. gulp.watch("scss/editormd.logo.scss", ["scss", "scss3"]);
  304. gulp.watch("src/editormd.js", ["js", "amd"]);
  305. });
  306. gulp.task("default", function() {
  307. gulp.run("scss");
  308. gulp.run("scss2");
  309. gulp.run("scss3");
  310. gulp.run("js");
  311. gulp.run("amd");
  312. gulp.run("cm-addon");
  313. gulp.run("cm-mode");
  314. });