Skip to content

Commit

Permalink
Merge pull request #10 from erikdesjardins/update
Browse files Browse the repository at this point in the history
Update, enable closure
erikdesjardins authored Dec 4, 2017
2 parents 8f87f48 + 180761a commit 675c981
Showing 5 changed files with 38 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM trzeci/emscripten-slim:sdk-tag-1.37.22-64bit
FROM trzeci/emscripten:sdk-tag-1.37.23-64bit

RUN apt-get update
RUN apt-get install gperf
14 changes: 2 additions & 12 deletions build.sh
Original file line number Diff line number Diff line change
@@ -14,16 +14,13 @@ gperf src/html_entities.gperf > build/html_entities.c
files="snudown.c src/autolink.c src/buffer.c src/markdown.c src/stack.c html/houdini_href_e.c html/houdini_html_e.c html/html.c build/html_entities.c"
include=("src" "html")
exported=("default_renderer" "wiki_renderer" "snudown_md" "version" "main")
exported_runtime=("ccall" "cwrap" "lengthBytesUTF8" "allocate" "intArrayFromString" "ALLOC_NORMAL" "Pointer_stringify" "UTF8ToString")
# ELIMINATE_DUPLICATE_FUNCTIONS=1 is expensive and only saves a few kB
# MEM_INIT_METHOD=2 may not compile correctly on Windows
options=("ERROR_ON_UNDEFINED_SYMBOLS=1" "NO_EXIT_RUNTIME=1" "NO_FILESYSTEM=1" "NO_DYNAMIC_EXECUTION=1" "MEM_INIT_METHOD=2" "NODEJS_CATCH_EXIT=0")
options=("ERROR_ON_UNDEFINED_SYMBOLS=1" "NO_EXIT_RUNTIME=1" "EXPORTED_RUNTIME_METHODS=[]" "NO_FILESYSTEM=1" "MEM_INIT_METHOD=2" "NODEJS_CATCH_EXIT=0")

if [ "$1" = "-d" ] || [ "$1" = "--debug" ]; then
optimization=""
else
echo "*** DEBUGGING OFF : use -d to enable ***"
optimization="-Oz --llvm-lto 1 --memory-init-file 0 -DNDEBUG -s ABORTING_MALLOC=0"
optimization="-Oz --llvm-lto 1 --closure 1 --memory-init-file 0 -DNDEBUG -s ABORTING_MALLOC=0"
fi

cmd="emcc $files -o build/snudown.js --pre-js header.js --post-js footer.js $optimization"
@@ -43,13 +40,6 @@ done
cmd="${cmd:0:${#cmd} - 1}" # trim last comma
cmd="$cmd]"

cmd="$cmd -s EXPORTED_RUNTIME_METHODS=["
for i in "${exported_runtime[@]}"; do
cmd="$cmd'$i',"
done
cmd="${cmd:0:${#cmd} - 1}" # trim last comma
cmd="$cmd]"

$cmd

# Remove `require()` calls from compiled code
63 changes: 31 additions & 32 deletions footer.js
Original file line number Diff line number Diff line change
@@ -3,23 +3,23 @@
* Equivalent to python: `__version__`.
* @type {string}
*/
var version = Module.ccall('version', 'string');
var version = UTF8ToString(asm['_version']());

/**
* The index of the usertext renderer.
* Can be passed to {@link markdown}.
* Equivalent to python: `RENDERER_USERTEXT`.
* @type {number}
*/
var RENDERER_USERTEXT = Module.ccall('default_renderer', 'number');
var RENDERER_USERTEXT = asm['_default_renderer']();

/**
* The index of the wiki renderer.
* Can be passed to {@link markdown}.
* Equivalent to python: `RENDERER_WIKI`.
* @type {number}
*/
var RENDERER_WIKI = Module.ccall('wiki_renderer', 'number');
var RENDERER_WIKI = asm['_wiki_renderer']();

/**
* Render markdown `text` to an HTML string.
@@ -36,12 +36,12 @@ var RENDERER_WIKI = Module.ccall('wiki_renderer', 'number');
*/
function markdown(text, nofollow, target, renderer, enableToc, tocIdPrefix) {
if (typeof text === 'object' && text !== null) {
nofollow = text.nofollow;
target = text.target;
renderer = text.renderer;
enableToc = text.enableToc;
tocIdPrefix = text.tocIdPrefix;
text = text.text;
nofollow = text['nofollow'];
target = text['target'];
renderer = text['renderer'];
enableToc = text['enableToc'];
tocIdPrefix = text['tocIdPrefix'];
text = text['text'];
}
if (typeof text !== 'string') {
text = '';
@@ -63,24 +63,18 @@ function markdown(text, nofollow, target, renderer, enableToc, tocIdPrefix) {
*/
function markdownWiki(text, nofollow, target, enableToc, tocIdPrefix) {
if (typeof text === 'object' && text !== null) {
nofollow = text.nofollow;
target = text.target;
enableToc = text.enableToc;
tocIdPrefix = text.tocIdPrefix;
text = text.text;
nofollow = text['nofollow'];
target = text['target'];
enableToc = text['enableToc'];
tocIdPrefix = text['tocIdPrefix'];
text = text['text'];
}
if (typeof text !== 'string') {
text = '';
}
return _markdown(text, nofollow, target, tocIdPrefix, RENDERER_WIKI, enableToc);
}

/**
* @private
* @returns {number} A pointer to the rendered string.
*/
var __markdown = Module.cwrap('snudown_md', 'number', ['number', 'number', 'number', 'string', 'string', 'number', 'number']);

/**
* @private
* @param {string} text
@@ -93,23 +87,28 @@ var __markdown = Module.cwrap('snudown_md', 'number', ['number', 'number', 'numb
*/
function _markdown(text, nofollow, target, toc_id_prefix, renderer, enable_toc) {
// not using Emscripten's automatic string handling since 'text'.length is unreliable for UTF-8
var size = Module.lengthBytesUTF8(text); // excludes null terminator
var buf = Module.allocate(Module.intArrayFromString(text), 'i8', Module.ALLOC_NORMAL);
var str = __markdown(buf, size, nofollow, target, toc_id_prefix, renderer, enable_toc);
Module._free(buf);
var string = Module.Pointer_stringify(str);
Module._free(str);
var size = lengthBytesUTF8(text); // excludes null terminator
var buf = allocate(intArrayFromString(text), 'i8', ALLOC_NORMAL);
var str = ccall(
'snudown_md',
'number',
['number', 'number', 'number', 'string', 'string', 'number', 'number'],
[buf, size, nofollow, target, toc_id_prefix, renderer, enable_toc]
);
asm['_free'](buf);
var string = UTF8ToString(str);
asm['_free'](str);
return string;
}

exports.version = version;
exports.RENDERER_USERTEXT = RENDERER_USERTEXT;
exports.RENDERER_WIKI = RENDERER_WIKI;
exports.markdown = markdown;
exports.markdownWiki = markdownWiki;
exports['version'] = version;
exports['RENDERER_USERTEXT'] = RENDERER_USERTEXT;
exports['RENDERER_WIKI'] = RENDERER_WIKI;
exports['markdown'] = markdown;
exports['markdownWiki'] = markdownWiki;

if (typeof define === 'function') {
define(exports);
}
})(typeof exports !== 'undefined' ? exports : typeof window !== 'undefined' ? (window.Snudown = {}) : {});
})(typeof exports !== 'undefined' ? exports : typeof window !== 'undefined' ? (window['Snudown'] = {}) : {});

4 changes: 2 additions & 2 deletions header.js
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
*/

(function(exports) {'use strict';
var Module = {
ENVIRONMENT: 'WEB'
Module = {
'ENVIRONMENT': 'WEB'
};

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "snudown-js",
"version": "2.0.3",
"version": "2.0.4",
"description": "a 'native' port of Snudown to JavaScript",
"main": "dist/snudown.js",
"repository": {
"type": "git",
"url": "https://github.com/erikdesjardins/snudown-js.git"
},
"devDependencies": {
"uglify-js": "3.1.7"
"uglify-js": "3.2.0"
},
"license": "MIT"
}

0 comments on commit 675c981

Please sign in to comment.