diff --git a/Gulpfile.js b/Gulpfile.js index e1ba05a..bb6ccde 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -90,6 +90,7 @@ gulp.task('devToProHtml', function() { .pipe(replace(//, '')) // 替换 .pipe(replace(//, '')) // 替换 .pipe(replace(//, "var tinyMCEPreInit = {base: '/public/tinymce', suffix: '.min'};")) // 替换 + .pipe(replace(/plugins\/main.js/, "plugins/main.min.js")) // 替换 // 连续两个空行换成一个空行, 没用 .pipe(replace(/\n\n/g, '\n')) .pipe(replace(/\n\n/g, '\n')) @@ -99,9 +100,180 @@ gulp.task('devToProHtml', function() { .pipe(gulp.dest(noteProBase)); }); -// tinymce +// Get used keys +// 只获取需要js i18n的key +var path = require('path'); +gulp.task('i18n', function() { + var keys = {}; + var reg = /getMsg\(["']+(.+?)["']+/g; + function getKey(data) { + while(ret = reg.exec(data)) { + keys[ret[1]] = 1; + } + } + // 先获取需要的key + function ls(ff) { + var files = fs.readdirSync(ff); + for(fn in files) { + var fname = ff + path.sep + files[fn]; + var stat = fs.lstatSync(fname); + if(stat.isDirectory() == true) { + ls(fname); + } + else { + if ((fname.indexOf('.html') > 0 || fname.indexOf('.js') > 0)) { + // console.log(fname); + // if (fname.indexOf('min.js') < 0) { + var data = fs.readFileSync(fname, "utf-8"); + // 得到getMsg里的key + getKey(data); + // } + } + } + } + } -var tinymceBase = base + '/tinymce_4.1.9'; + console.log('parsing used keys'); + + ls(base + '/admin'); + ls(base + '/blog'); + ls(base + '/dist'); + ls(base + '/js'); + ls(base + '/libs'); + ls(base + '/member'); + ls(base + '/tinymce'); + + console.log('parsed'); + + // msg.zh + function getAllMsgs(fname) { + var msg = {}; + + var data = fs.readFileSync(fname, "utf-8"); + var lines = data.split('\n'); + for (var i = 0; i < lines.length; ++i) { + var line = lines[i]; + // 忽略注释 + if (line[0] == '#' || line[1] == '#') { + continue; + } + var lineArr = line.split('='); + if (lineArr.length >= 2) { + var key = lineArr[0]; + lineArr.shift(); + msg[key] = lineArr.join('='); + // msg[lineArr[0]] = lineArr[1]; + } + } + return msg; + } + + // msg.zh, msg.js + function genI18nJsFile(fromFilename, keys) { + var msgs = getAllMsgs(leanoteBase + '/messages/' + fromFilename); + var toFilename = fromFilename + '.js'; + var toMsgs = {}; + for (var i in msgs) { + // 只要需要的 + if (i in keys) { + toMsgs[i] = msgs[i]; + } + } + var str = 'var MSG=' + JSON.stringify(toMsgs) + ';'; + str += 'function getMsg(key, data) {var msg = MSG[key];if(msg) {if(data) {if(!isArray(data)) {data = [data];}' + + 'for(var i = 0; i < data.length; ++i) {' + + 'msg = msg.replace("%s", data[i]);' + + '}' + + '}' + + 'return msg;' + + '}' + + 'return key;' + + '}'; + // 写入到文件中 + fs.writeFile(base + '/js/i18n/' + toFilename, str); + } + + genI18nJsFile('msg.zh', keys); + genI18nJsFile('msg.en', keys); + genI18nJsFile('msg.fr', keys); + genI18nJsFile('blog.zh', keys); + genI18nJsFile('blog.en', keys); + genI18nJsFile('blog.fr', keys); +}); + + +// plugins压缩 +gulp.task('plugins', function() { + gulp.src(base + '/js/plugins/libs/*.js') + .pipe(uglify()) // 压缩 + // .pipe(concat('main.min.js')) + .pipe(gulp.dest(base + '/js/plugins/libs-min')); + + + // 所有js合并成一个 + var jss = [ + 'note_info', + 'tips', + 'history', + 'attachment_upload', + 'editor_drop_paste', + 'main' + ]; + + for(var i in jss) { + jss[i] = base + '/js/plugins/' + jss[i] + '.js'; + } + + gulp.src(jss) + .pipe(uglify()) // 压缩 + .pipe(concat('main.min.js')) + .pipe(gulp.dest(base + '/js/plugins')); +}); + + +// mincss +var minifycss = require('gulp-minify-css'); +gulp.task('minifycss', function() { + gulp.src(base + '/css/bootstrap.css') + .pipe(rename({suffix: '-min'})) + .pipe(minifycss()) + .pipe(gulp.dest(base + '/css')); + + gulp.src(base + '/css/font-awesome-4.2.0/css/font-awesome.css') + .pipe(rename({suffix: '-min'})) + .pipe(minifycss()) + .pipe(gulp.dest(base + '/css/font-awesome-4.2.0/css')); + + gulp.src(base + '/css/zTreeStyle/zTreeStyle.css') + .pipe(rename({suffix: '-min'})) + .pipe(minifycss()) + .pipe(gulp.dest(base + '/css/zTreeStyle')); + + gulp.src(base + '/dist/themes/default.css') + .pipe(rename({suffix: '-min'})) + .pipe(minifycss()) + .pipe(gulp.dest(base + '/dist/themes')); + + gulp.src(base + '/js/contextmenu/css/contextmenu.css') + .pipe(rename({suffix: '-min'})) + .pipe(minifycss()) + .pipe(gulp.dest(base + '/js/contextmenu/css')); + + // theme + // 用codekit + var as = ['default', 'simple', 'writting', /*'writting-overwrite', */ 'mobile']; + /* + for(var i = 0; i < as.length; ++i) { + gulp.src(base + '/css/theme/' + as[i] + '.css') + .pipe(minifycss()) + .pipe(gulp.dest(base + '/css/theme')); + } + */ +}); + +// tinymce +// !! You must has tinymce_dev on public/ +var tinymceBase = base + '/tinymce_dev'; gulp.task('tinymce', function() { // 先清理 fs.unlink(tinymceBase + '/js/tinymce/tinymce.dev.js'); @@ -126,6 +298,7 @@ gulp.task('tinymce', function() { }); // 合并css, 无用 +// Deprecated gulp.task('concatCss', function() { return gulp .src([markdownRaw + '/css/default.css', markdownRaw + '/css/md.css']) @@ -135,4 +308,4 @@ gulp.task('concatCss', function() { gulp.task('concat', ['concatDepJs', 'concatAppJs', 'concatMarkdownJs']); gulp.task('html', ['devToProHtml']); -gulp.task('default', ['concat', 'html']); +gulp.task('default', ['concat', 'plugins', 'minifycss', 'i18n', 'html']); diff --git a/app/controllers/NoteController.go b/app/controllers/NoteController.go index 1bc6348..88070d5 100644 --- a/app/controllers/NoteController.go +++ b/app/controllers/NoteController.go @@ -126,7 +126,7 @@ func (c Note) Index(noteId, online string) revel.Result { c.RenderArgs["globalConfigs"] = configService.GetGlobalConfigForUser() // return c.RenderTemplate("note/note.html") - + if isDev, _ := revel.Config.Bool("mode.dev"); isDev && online == "" { return c.RenderTemplate("note/note-dev.html") } else { diff --git a/app/views/note/note.html b/app/views/note/note.html index d24f311..46e8d58 100644 --- a/app/views/note/note.html +++ b/app/views/note/note.html @@ -10,23 +10,23 @@