diff --git a/.gitignore b/.gitignore index d543800..225e7c1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ node_modules out bower_components +bundle.js +nml.js diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1fac5f0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +The MIT License (MIT) +Copyright © 2016 AI-create-team + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index eb33ba6..a52d83a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,45 @@ -# NML-JavaScript -NML to HTML/PlainText for JavaScript by @kawakawaritsuki +# NML-Converter +## What's this? +NML to HTML/PlainText for JavaScript. -NMLをいろいろと変換するためのnpmパッケージ。 +## How to use +Install +``` +npm install nml-converter +``` +Use +``` +var NML = require('nml-converter') -このリンクから試せます。 -http://writerlighter.github.io/NML-JavaScript/ +var novel = new NML("# NML format\nExample!") + +console.log(novel.to("html")) +# or +console.log(novel.to("plain")) +``` + +## How to build + +``` +$ npm install +$ bower install +$ gulp +``` + + +## Test now! +http://WriterLighter.github.io/NML-JavaScript/ + +## Contributor +@kawakawaritsuki + +## License + The MIT License (MIT) + +Copyright © 2016 ai-create-team + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..81b74c1 --- /dev/null +++ b/bower.json @@ -0,0 +1,35 @@ +{ + "name": "nml-converter", + "version": "1.0.3", + "description": "NML(Novel Markup Language) converter for js", + "main": "nml.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/WriterLighter/NML-JavaScript.git" + }, + "keywords": [ + "nml", + "converter", + "novel", + "markup" + ], + "author": "Ritsuki Goto (http://kawakawaplanning.xyz/)", + "license": "MIT", + "bugs": { + "url": "https://github.com/WriterLighter/NML-JavaScript/issues" + }, + "homepage": "https://github.com/WriterLighter/NML-JavaScript#readme", + "devDependencies": { + "browserify": "^13.0.0", + "coffee-script": "^1.10.0", + "gulp": "^3.9.0", + "gulp-coffee": "^2.3.1", + "gulp-plumber": "^1.1.0", + "gulp-concat": "^2.6.0", + "gulp-watch": "^4.3.5", + "gulp-notify": "^2.2.0" + } +} diff --git a/bundle.js b/bundle.js deleted file mode 100644 index 042e8ad..0000000 --- a/bundle.js +++ /dev/null @@ -1,282 +0,0 @@ -(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o"; - }else{ - output.innerHTML = novel.to(mode.options[mode.selectedIndex].value); - } - - -} - -$(function(){ - convert(); -}); - -$(function(){ - $("#textarea").bind("keyup", function(){ - convert(); - }); -}); - -},{"./out/nml":2}],2:[function(require,module,exports){ -(function() { - module.exports = this.NML = (function() { - var checkBlockquotes, checkBold, checkItalic, checkLine, checkLink, checkNewPage, checkPunctuationNumber, checkPunctuationSymbol, checkReturn, checkRuby, checkSharp, checkSpace, checkStrikethrough, mode; - - mode = 0; - - function NML(text1) { - this.text = text1; - } - - NML.prototype.to = function(t) { - var body, s; - switch (t) { - case "html": - mode = 1; - s = ''; - body = this.text.split('\n'); - body.forEach(function(text1) { - this.text = text1; - return s = s + checkLine(this.text) + '\n'; - }); - return '
' + s + '
'; - case "plain": - mode = 2; - s = ''; - body = this.text.split('\n'); - body.forEach(function(text1) { - this.text = text1; - return s = s + checkLine(this.text) + '\n'; - }); - return s; - default: - return "Error!"; - } - }; - - checkLine = function(line) { - line = checkSpace(line); - line = checkReturn(line); - line = checkRuby(line); - line = checkNewPage(line); - line = checkSharp(line); - line = checkStrikethrough(line); - line = checkItalic(line); - line = checkBold(line); - line = checkBlockquotes(line); - line = checkLink(line); - line = checkPunctuationNumber(line); - line = checkPunctuationSymbol(line); - return line; - }; - - checkSpace = function(line) { - if (line.match(/^[ \s]/)) { - if (mode === 1) { - line = '

' + line + '

'; - } - } - return line; - }; - - checkReturn = function(line) { - if (line === '') { - if (mode === 1) { - line = '
'; - } - } - return line; - }; - - checkRuby = function(line) { - var moji, myArray, myRe, ruby, text; - myRe = /[\||].*?[\((].*?[\))]/g; - myArray = void 0; - while ((myArray = myRe.exec(line)) !== null) { - text = myArray[0]; - if (mode === 1) { - moji = text.match(/[\||].*?[\((]/)[0]; - moji = moji.substring(1, moji.length - 1); - ruby = text.match(/[\((].*?[\))]/)[0]; - ruby = ruby.substring(1, ruby.length - 1); - line = line.replace(text, '' + moji + '' + ruby + ''); - } else { - moji = text.match(/[\||].*?[\((]/)[0]; - moji = moji.substring(1, moji.length - 1); - line = line.replace(text, moji); - } - } - return line; - }; - - checkNewPage = function(line) { - if (line.match(/^[-ーis]{3,}$/)) { - if (mode === 1) { - line = '
'; - } else if (mode === 2) { - line = ''; - } - } - return line; - }; - - checkSharp = function(line) { - var count, md; - if (mode === 1) { - md = void 0; - if ((md = line.match(/^[##]*/)[0]) !== '') { - count = md.length > 6 ? 6 : md.length; - line = line.replace(/^[##]*/, ''); - line = '' + line + ''; - } - } else if (mode === 2) { - line = line.replace(/^[##]*/, ''); - } - return line; - }; - - checkStrikethrough = function(line) { - var myArray, myRe, text; - myRe = /[\~〜]{2}.*?[\~〜]{2}/g; - myArray = void 0; - while ((myArray = myRe.exec(line)) !== null) { - text = myArray[0]; - if (mode === 1) { - line = line.replace(text, '' + text.substring(2, text.length - 2) + ''); - } else if (mode === 2) { - line = line.replace(text, text.substring(2, text.length - 2)); - } - } - return line; - }; - - checkItalic = function(line) { - var myArray, myRe, text; - myRe = /[\__**]{1}.*?[\__**]{1}/g; - myArray = void 0; - while ((myArray = myRe.exec(line)) !== null) { - text = myArray[0]; - if (mode === 1) { - if (!text.match(/^[\__**]{2}$/)) { - line = line.replace(text, '' + text.substring(1, text.length - 1) + ''); - } - } else if (mode === 2) { - if (!text.match(/^[\__**]{2}$/)) { - line = line.replace(text, text.substring(1, text.length - 1)); - } - } - } - return line; - }; - - checkBold = function(line) { - var myArray, myRe, text; - myRe = /[\__**]{2}.*?[\__**]{2}/g; - myArray = void 0; - while ((myArray = myRe.exec(line)) !== null) { - text = myArray[0]; - if (mode === 1) { - line = line.replace(text, '' + text.substring(2, text.length - 2) + ''); - } else if (mode === 2) { - line = line.replace(text, text.substring(2, text.length - 2)); - } - } - return line; - }; - - checkBlockquotes = function(line) { - if (line.match(/^[>>]/)) { - if (mode === 1) { - line = '
' + line.substring(1) + '
'; - } - } - return line; - }; - - checkLink = function(line) { - var linkText, myArray, myRe, name, text, url; - myRe = /[!!]*\[.*?\][\((].*?[\))]/g; - myArray = void 0; - while ((myArray = myRe.exec(line)) !== null) { - text = myArray[0]; - if (text.match(/[!!]{1,}\[.*?\][\((].*?[\))]/)) { - if (mode === 1) { - if (text.match(/\".*\"/)) { - linkText = line.match(/\[.*?\]/)[0]; - url = line.match(/[\((].*?[\"]/)[0].replace(RegExp(' ', 'g'), ''); - name = line.match(/[\"].*?[\"]/)[0]; - linkText = linkText.substring(1, linkText.length - 1); - url = url.substring(1, url.length - 1); - name = name.substring(1, name.length - 1); - line = line.replace(text, '' + linkText + ''); - } else { - linkText = line.match(/\[.*?\]/)[0]; - url = line.match(/[\((].*?[\))]/)[0].replace(RegExp(' ', 'g'), ''); - linkText = linkText.substring(1, linkText.length - 1); - url = url.substring(1, url.length - 1); - line = line.replace(text, '' + linkText + ''); - } - } else if (mode === 2) { - line = line.replace(text, ""); - } - } else { - if (mode === 1) { - if (text.match(/\".*\"/)) { - linkText = line.match(/\[.*?\]/)[0]; - url = line.match(/[\((].*?[\"]/)[0].replace(RegExp(' ', 'g'), ''); - name = line.match(/[\"].*?[\"]/)[0]; - linkText = linkText.substring(1, linkText.length - 1); - url = url.substring(1, url.length - 1); - name = name.substring(1, name.length - 1); - line = line.replace(text, '' + linkText + ''); - } else { - linkText = line.match(/\[.*?\]/)[0]; - url = line.match(/[\((].*?[\))]/)[0].replace(RegExp(' ', 'g'), ''); - linkText = linkText.substring(1, linkText.length - 1); - url = url.substring(1, url.length - 1); - line = line.replace(text, '' + linkText + ''); - } - } else if (mode === 2) { - line = line.replace(text, ""); - } - } - } - return line; - }; - - checkPunctuationNumber = function(line) { - if (line.match(/^[0-9]+\.$/)) { - if (mode === 1) { - line = '
'; - } - } - return line; - }; - - checkPunctuationSymbol = function(line) { - if (line.match(/^[-*ー*]+\.$/)) { - if (mode === 1) { - line = '
'; - } - } - return line; - }; - - return NML; - - })(); - -}).call(this); - -},{}]},{},[1]); diff --git a/gulpfile.coffee b/gulpfile.coffee index d18d80c..0dae87c 100644 --- a/gulpfile.coffee +++ b/gulpfile.coffee @@ -10,7 +10,7 @@ gulp.task 'nml', -> .pipe(plumber()) .pipe(coffee()) .pipe(concat('nml.js')) - .pipe(gulp.dest('out')) + .pipe(gulp.dest('')) .pipe notify 'nml.js done!!!', {onLast: true} gulp.task 'watch', ['default'], -> diff --git a/index.html b/index.html deleted file mode 100644 index bb6b0b0..0000000 --- a/index.html +++ /dev/null @@ -1,71 +0,0 @@ - - - NML-Converter - - - - - - - -
- NML Convert to -
- -
-
- -
-
- -
-
-
-
-
-
-
-
- - - diff --git a/main.js b/main.js deleted file mode 100644 index b7445f3..0000000 --- a/main.js +++ /dev/null @@ -1,28 +0,0 @@ -NML = require("./out/nml") - -window.convert = function() { - - var textarea = document.getElementsByTagName('textarea')[0]; - var mode = document.getElementById('mode'); - - var novel = new NML(textarea.value) - var output = document.getElementById("output"); - - if (mode.options[mode.selectedIndex].value === "plain"){ - output.innerHTML = "
" + novel.to(mode.options[mode.selectedIndex].value) + "
"; - }else{ - output.innerHTML = novel.to(mode.options[mode.selectedIndex].value); - } - - -} - -$(function(){ - convert(); -}); - -$(function(){ - $("#textarea").bind("keyup", function(){ - convert(); - }); -}); diff --git a/nml.css b/nml.css deleted file mode 100644 index cd8210d..0000000 --- a/nml.css +++ /dev/null @@ -1,149 +0,0 @@ -html, body { - height: 100%; - padding: 0; - margin: 0; -} -textarea { - resize: none; - height: 100%; - width: 100%; - padding: 1em 1em; - background-color: #E0F2F1; - font-family: sans-serif; - border:0px; - font-size: 1em; -} -pre { - font-family: sans-serif; - font-size: 1em; -} -.meg { - width: 10%; - align-items: stretch; - float: left; - height: 100%; -} -.out{ - padding: 0em 1em; - height: 100%; - width: 100%; - word-wrap: break-word; - overflow: auto; - background-color: #B2DFDB; -} -.form { - align-items: stretch; - float: left; - width: 80%; - margin-top: 0.5em; - margin-bottom: 3em; -} -.formarea { - float: left; - width: 49%; -} -.split { - float: left; - width: 0.5em; - height: 100%; - background-color: #FFF; -} -.title{ - width: 100%; - height: 1.5em; - font-size: 3em; - color: #fff; - background-color: #009688; - text-align: center; -} -.footer{ - line-height:1.2; - z-index:9999; - text-align:center; - position:fixed; - bottom:0; - left:0; - width:100%; - padding:8px; - background:#222222; - color: #FFF; - font-size:1em; - background-color:rgba(0,0,0,0.7); -} -.footer-link{ - color: #FFF -} - -.dropdown-select { - position: relative; - width: 130%; - margin: 0; - padding: 6px 8px 6px 10px; - height: 28px; - line-height: 14px; - font-size: 12px; - color: #62717a; - text-shadow: 0 1px white; - /* Fallback for IE 8 */ - background: #f2f2f2; - /* "transparent" doesn't work with Opera */ - background: rgba(0, 0, 0, 0) !important; - border: 0; - border-radius: 0; - -webkit-appearance: none; -} -.dropdown-select:focus { - z-index: 3; - width: 100%; - color: #394349; - outline: 2px solid #49aff2; - outline: 2px solid -webkit-focus-ring-color; - outline-offset: -2px; -} -.dropdown-select > option { - margin: 3px; - padding: 6px 8px; - text-shadow: none; - background: #f2f2f2; - border-radius: 3px; - cursor: pointer; -} - -.dropdown { - display: inline-block; - position: relative; - overflow: hidden; - height: 28px; - width: 150px; - background: #f2f2f2; - border: 1px solid; - border-color: white #f7f7f7 #f5f5f5; - border-radius: 3px; - background-image: -webkit-linear-gradient(top, transparent, rgba(0, 0, 0, 0.06)); - background-image: -moz-linear-gradient(top, transparent, rgba(0, 0, 0, 0.06)); - background-image: -o-linear-gradient(top, transparent, rgba(0, 0, 0, 0.06)); - background-image: linear-gradient(to bottom, transparent, rgba(0, 0, 0, 0.06)); - -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08); - box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08); -} -.dropdown:before, .dropdown:after { - content: ''; - position: absolute; - z-index: 2; - top: 9px; - right: 10px; - width: 0; - height: 0; - border: 4px dashed; - border-color: #888 transparent; - pointer-events: none; -} -.dropdown:before { - border-bottom-style: solid; - border-top: none; -} -.dropdown:after { - margin-top: 7px; - border-top-style: solid; - border-bottom: none; -} diff --git a/package.json b/package.json new file mode 100644 index 0000000..81b74c1 --- /dev/null +++ b/package.json @@ -0,0 +1,35 @@ +{ + "name": "nml-converter", + "version": "1.0.3", + "description": "NML(Novel Markup Language) converter for js", + "main": "nml.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/WriterLighter/NML-JavaScript.git" + }, + "keywords": [ + "nml", + "converter", + "novel", + "markup" + ], + "author": "Ritsuki Goto (http://kawakawaplanning.xyz/)", + "license": "MIT", + "bugs": { + "url": "https://github.com/WriterLighter/NML-JavaScript/issues" + }, + "homepage": "https://github.com/WriterLighter/NML-JavaScript#readme", + "devDependencies": { + "browserify": "^13.0.0", + "coffee-script": "^1.10.0", + "gulp": "^3.9.0", + "gulp-coffee": "^2.3.1", + "gulp-plumber": "^1.1.0", + "gulp-concat": "^2.6.0", + "gulp-watch": "^4.3.5", + "gulp-notify": "^2.2.0" + } +} diff --git a/src/nml.coffee b/src/nml.coffee index 5e93812..03a6a72 100644 --- a/src/nml.coffee +++ b/src/nml.coffee @@ -4,6 +4,14 @@ module.exports = class @NML # mode # 1:html # 2:plain + multi_line = "" + multi_line_mode = 0 + # mode + # 0:not multi line + # 1:html + # 2:plain + + list_indent = -1 constructor: (@text) -> @@ -15,13 +23,16 @@ module.exports = class @NML body = @text.split('\n') body.forEach (@text) -> s = s + checkLine(@text) + '\n' - return '
' + s + '
' + s = checkRemoveDiv('
' + s.substr( 0, s.length-1 ) + '
') + + return s when "plain" mode = 2 s = '' body = @text.split('\n') body.forEach (@text) -> s = s + checkLine(@text) + '\n' + s.substr( 0, s.length-1 ) return s else "Error!" @@ -39,20 +50,22 @@ module.exports = class @NML line = checkLink(line) line = checkPunctuationNumber(line) line = checkPunctuationSymbol(line) + line = checkNextLine(line) + line = checkDefinitionList(list) line #形式段落 checkSpace = (line) -> if line.match(/^[ \s]/) if mode is 1 - line = '

' + line + '

' + line = '

' + line.replace(/^[ \s]/,"") + '

' line #意味段落 checkReturn = (line) -> if line is '' if mode is 1 - line = '
' + line = '
' line #ルビ @@ -66,18 +79,20 @@ module.exports = class @NML moji = moji.substring(1, moji.length - 1) ruby = text.match(/[\((].*?[\))]/)[0] ruby = ruby.substring(1, ruby.length - 1) - line = line.replace(text, '' + moji + '' + ruby + '') + line = line.replace(text, '' + moji + '' + ruby + '') else moji = text.match(/[\||].*?[\((]/)[0] moji = moji.substring(1, moji.length - 1) - line = line.replace(text, moji) + ruby = text.match(/[\((].*?[\))]/)[0] + ruby = ruby.substring(1, ruby.length - 1) + line = line.replace(text, moji + "(" + ruby + ")") line #改ページ checkNewPage = (line) -> if line.match(/^[-ーis]{3,}$/) if mode is 1 - line = '
' + line = '
' else if mode is 2 line = '' line @@ -89,7 +104,7 @@ module.exports = class @NML if (md = line.match(/^[##]*/)[0]) != '' count = if md.length > 6 then 6 else md.length line = line.replace(/^[##]*/, '') - line = '' + line + '' + line = '' + line.replace(/^[ \s]/,"") + '' else if mode is 2 line = line.replace(/^[##]*/, '') line @@ -107,7 +122,6 @@ module.exports = class @NML line #斜体 - checkItalic = (line) -> myRe = /[\__**]{1}.*?[\__**]{1}/g myArray = undefined @@ -155,13 +169,13 @@ module.exports = class @NML linkText = linkText.substring(1, linkText.length - 1) url = url.substring(1, url.length - 1) name = name.substring(1, name.length - 1) - line = line.replace(text, '' + linkText + '') + line = line.replace(text, '' + linkText + '') else linkText = line.match(/\[.*?\]/)[0] url = line.match(/[\((].*?[\))]/)[0].replace(RegExp(' ', 'g'), '') linkText = linkText.substring(1, linkText.length - 1) url = url.substring(1, url.length - 1) - line = line.replace(text, '' + linkText + '') + line = line.replace(text, '' + linkText + '') else if mode is 2 line = line.replace(text, "") @@ -189,12 +203,25 @@ module.exports = class @NML checkPunctuationNumber = (line) -> if line.match(/^[0-9]+\.$/) if mode is 1 - line = '
' + line = '
' line #記号区切り checkPunctuationSymbol = (line) -> if line.match(/^[-*ー*]+\.$/) if mode is 1 - line = '
' + line = '
' + line + + #改行 + checkNextLine = (line) -> + if line.match(/[ \s]{3,}$/) + line = line + "
" line + + #冗長なdivを削除 + checkRemoveDiv = (line) -> + while (line.match(/
<\/div>/)) + line = line.replace(/
<\/div>/, '') + line +