Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten (most) of the internal modules #51

Merged
merged 1 commit into from
Apr 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cli/tinydoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var program = require('commander');
var fs = require('fs-extra');
var path = require('path');
var deep = require('deep-get-set');
var set = require('lodash').set;
var pkg = require('../package');
var tinydoc = require('..');
var Logger = require('../lib/Logger');
Expand Down Expand Up @@ -69,7 +69,7 @@ program.override.forEach(function(override) {
var key = fragments[0];
var value = JSON.parse(fragments[1]);

deep(config, key, value);
set(config, key, value);

console.log('Overridden "%s" with "%s"', key, value);
});
Expand Down
32 changes: 15 additions & 17 deletions cli/tinydoc-compile
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
#!/usr/bin/env node

var program = require('commander');
var fs = require('fs-extra');
var assert = require('assert');
var path = require('path');
var deep = require('deep-get-set');
var pkg = require('../package');
var PluginCompiler = require('../lib/PluginCompiler');
var PluginCompiler = require('../lib/HTMLSerializer__PluginCompiler');
var ctx = {};

function resolvePath(arg) {
return path.resolve(arg);
}

program
.version(pkg.version)
.description('Compile a tinydoc UI plugin.')
.arguments('<outfile> <entry_file> [other_entry_files...]')
.option('--optimize', 'Build a production-ready version.')
.option('-v, --verbose', 'Shortcut for --log-level="info"')
.option('-d, --debug', 'Shortcut for --log-level="debug"')
.action(function(output, entry, otherEntries) {
ctx.output = output;
ctx.entry = [ entry ].concat(otherEntries);
Expand All @@ -31,12 +22,19 @@ if (!ctx.output || ctx.entry.length === 0) {
program.help();
}

PluginCompiler.compile(ctx.entry.map(resolvePath), resolvePath(ctx.output), program.optimize, function(err) {
if (err) {
throw err;
}
PluginCompiler.compile(
ctx.entry.map(resolvePath),
resolvePath(ctx.output),
program.optimize,
throwOnError
);

// process.stdin.end();
});
function resolvePath(arg) {
return path.resolve(arg);
}

// process.stdin.resume();
function throwOnError(e) {
if (e) {
throw e;
}
}
11 changes: 8 additions & 3 deletions lib/Utils.js → lib/AssetUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var path = require('path');
var fs = require('fs-extra');
var glob = require('glob');
var arrayWrap = require('./utils/arrayWrap');
var console = require('./Logger')('tinydoc');
var merge = require('lodash').merge;

Expand All @@ -13,7 +12,7 @@ var tmpFileId = 0;

/**
* @namespace Core
* @module Utils
* @module AssetUtils
*
* A bunch of utilities for dealing with asset files.
*
Expand All @@ -23,7 +22,7 @@ var tmpFileId = 0;
* @param {Object} config
* tinydoc's runtime config.
*/
module.exports = function createUtils(config) {
module.exports = function AssetUtils(config) {
var utils = {
/**
* Convert a relative asset path into an absolute path.
Expand Down Expand Up @@ -143,3 +142,9 @@ module.exports = function createUtils(config) {

return utils;
};

function arrayWrap(value) {
return Array.isArray(value) ? value : value !== undefined ? [ value ] : [];
}

module.exports.arrayWrap = arrayWrap;
6 changes: 3 additions & 3 deletions lib/Compiler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var fs = require('fs-extra');
var EventEmitter = require('events').EventEmitter;
var Utils = require('./Utils');
var AssetUtils = require('./AssetUtils');
var Assets = require('./Assets');
var Registry = require('./Registry');
var LinkResolver = require('./LinkResolver');
Expand Down Expand Up @@ -73,12 +73,12 @@ function Compiler(config) {
this.assets = new Assets();

/**
* @property {Core.Utils}
* @property {Core.AssetUtils}
*
* A set of path-sensitive utils that take the user-configuration into
* account.
*/
this.utils = new Utils(config);
this.utils = new AssetUtils(config);

/**
* @property {Core.Registry}
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/core/index.js → lib/HTMLSerializer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var write = require('./write');
var write = require('./HTMLSerializer__write');

exports.name = 'CorePlugin';
exports.name = 'HTMLPlugin';
exports.run = function(compiler) {
var config = compiler.config;
var database = {};
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var path = require('path');
var webpack = require('webpack');
var root = path.resolve(__dirname, '..', '..', '..', '..');
var root = path.resolve(__dirname, '..');

module.exports = function compileCSS(compiler, config, done) {
var files = compiler.assets.styleSheets;
Expand All @@ -16,17 +16,17 @@ module.exports = function compileCSS(compiler, config, done) {
jsonpFunction: 'webpackJsonp_CSS'
},

devtool: null,
devtool: 'inline-sourcemap',
entry: files,

resolve: {
extensions: [ '', '.less', '.css' ],
modulesDirectories: [ 'node_modules' ],
fallback: path.resolve(root, 'ui', 'css')
fallback: path.join(root, 'ui/css')
},

resolveLoader: {
root: path.resolve(root, 'node_modules'),
root: path.join(root, 'node_modules'),
},

module: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var PluginCompiler = require('../../../PluginCompiler');
var PluginCompiler = require('./HTMLSerializer__PluginCompiler');

module.exports = function compileInlinePlugins(compiler, config, done) {
PluginCompiler.compile(
Expand Down
10 changes: 5 additions & 5 deletions lib/plugins/core/write.js → lib/HTMLSerializer__write.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var fs = require('fs-extra');
var path = require('path');
var async = require('async');
var root = path.resolve(__dirname, '..', '..', '..');
var _ = require('lodash');
var compileCSS = require('./write/compileCSS');
var compileInlinePlugins = require('./write/compileInlinePlugins');
var CORE_SCRIPTS = ['config.js', 'vendor.js', 'main.js'];
var CORE_STYLE = path.resolve(root, 'ui', 'css', 'index.less');
var compileCSS = require('./HTMLSerializer__compileCSS');
var compileInlinePlugins = require('./HTMLSerializer__compileInlinePlugins');
var root = path.resolve(__dirname, '..');
var version = require(path.join(root, 'package')).version;
var CORE_SCRIPTS = ['config.js', 'vendor.js', 'main.js'];
var CORE_STYLE = path.resolve(root, 'ui/css/index.less');

module.exports = function(config, compiler, database, done) {
compiler.assets.styleSheets.unshift(CORE_STYLE);
Expand Down
27 changes: 18 additions & 9 deletions lib/Renderer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
var fs = require('fs');
var path = require('path');
var marked = require('marked');
var _ = require('lodash');
var React = require('react');
var Utils = require('./Renderer/Utils');
var RendererUtils = require('./RendererUtils');
var multiline = require('multiline-slash');
var assign = _.assign;

var AnchorableHeadingTmpl = _.template(
fs.readFileSync(path.resolve(__dirname, 'Renderer', 'anchorable-heading.tmpl.html'), 'utf-8')
multiline(function() {;
// <h<%- level %> class="markdown-text__heading anchorable-heading" id="<%- id %>">
// <a href="<%- href %>" class="markdown-text__heading-anchor icon icon-link"></a><span class="markdown-text__heading-title"><%= text %></span>
// </h<%- level %>>
})
);

var HeadingTmpl = _.template(
fs.readFileSync(path.resolve(__dirname, 'Renderer', 'heading.tmpl.html'), 'utf-8')
multiline(function() {;
// <h<%- level %> class="markdown-text__heading">
// <span class="markdown-text__heading-title"><%= text %></span>
// </h<%- level %>>
})
);

var RE_INTERNAL_LINK = Object.freeze(/^tiny:\/\//);
Expand Down Expand Up @@ -44,7 +51,9 @@ function createRenderer(config) {
// this could be heavily optimized, but meh for now
renderer.heading = function(text, level) {
var id = state.baseURL;
var scopedId = Utils.normalizeHeading(Utils.sanitize(text.split('\n')[0]));
var scopedId = RendererUtils.normalizeHeading(
RendererUtils.htmlToText(text.split('\n')[0])
);

if (level > 1) {
id += '/' + scopedId;
Expand All @@ -55,7 +64,7 @@ function createRenderer(config) {
scopedId: scopedId,
level: level,
html: text,
text: Utils.renderText(text).trim()
text: RendererUtils.markdownToText(text).trim()
});

if (runOptions.anchorableHeadings) {
Expand Down Expand Up @@ -101,7 +110,7 @@ function createRenderer(config) {
}));

if (options && options.trimHTML) {
html = Utils.trimHTML(html);
html = RendererUtils.trimHTML(html);
}

state.baseURL = '';
Expand All @@ -124,7 +133,7 @@ function createRenderer(config) {
toc = state.toc;

if (options && options.trimHTML) {
html = Utils.trimHTML(html);
html = RendererUtils.trimHTML(html);
}

state = createState();
Expand Down
3 changes: 0 additions & 3 deletions lib/Renderer/anchorable-heading.tmpl.html

This file was deleted.

3 changes: 0 additions & 3 deletions lib/Renderer/heading.tmpl.html

This file was deleted.

38 changes: 27 additions & 11 deletions lib/Renderer/Utils.js → lib/RendererUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
var htmlToText = require('../utils/htmlToText');
var markdownToText = require('../utils/markdownToText');
var marked = require('marked');
var summaryExtractor = SummaryExtractor();
var htmlparser = require("htmlparser2");

var markdownToTextOptions = Object.freeze({
tables: false,
smartLists: false,
sanitize: false,
breaks: false,
linkify: false
});

/**
* @module
Expand All @@ -15,8 +22,17 @@ var summaryExtractor = SummaryExtractor();
* @return {String}
* The string without any HTML entities.
*/
function sanitize(html) {
return htmlToText(html);
function htmlToText(html) {
var buf = '';
var parser = new htmlparser.Parser({
ontext: function(text){
buf += text;
}
}, {decodeEntities: true});

parser.write(html);

return buf;
}

/**
Expand All @@ -30,8 +46,8 @@ function sanitize(html) {
* @return {String}
* The plain-text (no HTML inside of it) string.
*/
function renderText(markdown) {
return markdownToText(markdown);
function markdownToText(md) {
return htmlToText(marked(md, markdownToTextOptions));
}

/**
Expand All @@ -45,7 +61,7 @@ function renderText(markdown) {
*
* @param {Boolean} [isMarkdown=true]
* If false, no implicit rendering to text is done. Pass this if you
* already called [renderText]() on your string or you know it contains
* already called [markdownToText]() on your string or you know it contains
* no markdown (or HTML).
*
* @return {String}
Expand Down Expand Up @@ -86,8 +102,8 @@ function extractSummary(markdown) {
return summaryExtractor(markdown);
}

exports.sanitize = sanitize;
exports.renderText = renderText;
exports.htmlToText = htmlToText;
exports.markdownToText = markdownToText;
exports.normalizeHeading = normalizeHeading;
exports.trimHTML = trimHTML;
exports.extractSummary = extractSummary;
Expand Down Expand Up @@ -122,6 +138,6 @@ function SummaryExtractor() {
text = state.text || '';
state = null;

return renderText('<p>' + text + '</p>');
return markdownToText('<p>' + text + '</p>');
};
}
}
Loading