HTMLエディタで使われるhtmlタグを青空文庫形式に変換。CKeditorやtinyMCEなどのHTMLを青空文庫形式に変換できるかも。電書協EPUBではhtmlタグでの違いで意味を取るより、spanとdivにクラスをつけてCSSの内容によって切り替えている。そのためclassの名前を読み込まないとちゃんとパースができない。
前回の続き 99nyorituryo.hatenablog.com
hr区切りを改ページにした
rules.horizontalRule = {
filter: 'hr',
replacement: function (content, node, options) {
return '\n\n[#改ページ]\n\n' //区切りを改ページに
}
}
linkタグをlinkタグに戻す何もしない処理
rules.inlineLink = {
filter: function (node, options) {
return (
options.linkStyle === 'inlined' &&
node.nodeName === 'A' &&
node.getAttribute('href')
)
},
replacement: function (content, node) {
var href = node.getAttribute('href')
var title = cleanAttribute(node.getAttribute('title'))
if (title) title = ' title="' + title +'" '
return '<a href="' + href + '"' + title+'>'+content + '</a>'
}
}
リストタグは[#ここから2字下げ、折り返して3字下げ]でいいかな、リストの頭に"一、", "● ",〇 "をつけて箇条書きっぽくする。
"bulletListMarker": "一、", "● ",〇 "
rules.list = {
filter: ['ul', 'ol'],
replacement: function (content, node) {
var parent = node.parentNode
return '[#ここから2字下げ、折り返して3字下げ]\n' + content + '\n[#ここで字下げ終わり]'
}
}
rules.listItem = {
filter: 'li',
replacement: function (content, node, options) {
content = content
.replace(/^\n+/, '') // remove leading newlines
.replace(/\n+$/, '\n') // replace trailing newlines with just a single one
.replace(/\n/gm, '\n ') // indent
var prefix = options.bulletListMarker
var parent = node.parentNode
if (parent.nodeName === 'OL') {
var start = parent.getAttribute('start')
var index = Array.prototype.indexOf.call(parent.children, node)
prefix = (start ? Number(start) + index : index + 1) + '. '
}
return (
prefix + content + (node.nextSibling && !/\n$/.test(content) ? '\n' : '')
)
}
}
blockquoteをここから2字下げ
rules.blockquote = {
filter: 'blockquote',
replacement: function (content) {
return '\n[#ここから2字下げ]' + content + '[#ここで字下げ終わり]\n'
}
}
コードブロックの処理、エスケープ文字の処理をするかしないか。preとcodeへの青空文庫での置換方法がない。なので取りあえず、「ここから〇〇言語」にしておく
<pre><code class="language-js"></code></pre>
rules.indentedCodeBlock = {
filter: function (node, options) {
return (
options.codeBlockStyle === 'indented' &&
node.nodeName === 'PRE' &&
node.firstChild &&
node.firstChild.nodeName === 'CODE'
)
},
replacement: function (content, node, options) {
return '\n[#ここから2字下げ]\n' + content + '\n[#ここで字下げ終わり]\n'
}
}
rules.fencedCodeBlock = {
filter: function (node, options) {
return (
options.codeBlockStyle === 'fenced' &&
node.nodeName === 'PRE' &&
node.firstChild &&
node.firstChild.nodeName === 'CODE'
)
},
replacement: function (content, node, options) {
var className = node.firstChild.getAttribute('class') || ''
var language = (className.match(/language-(\S+)/) || [null, ''])[1]
var code = node.firstChild.textContent
return (
'\n\n[#ここから' + language + '言語]\n' +
code.replace(/\n$/, '') +
'\n[#ここで' + language + '言語終わり]\n\n'
)
}
}
太字
傍点
傍線
斜体
下線
打消し線
下付き
上付き
bold strong em italic u del sub sup
太字、傍点、傍線、斜体、下線、打ち消し、下付き、上付き
rules.emphasis = {
filter: ['em'],
replacement: function (content, node, options) {
if (!content.trim()) return ''
return '[#'+ options.emDelimiter + ']'+ content + '[#'+ options.emDelimiter + '終わり]'
}
}
rules.italic = {
filter: [ 'i'],
replacement: function (content, node, options) {
if (!content.trim()) return ''
return '[#'+ options.italicDelimiter + ']'+ content + '[#'+ options.italicDelimiter + '終わり]'
}
}
rules.strong = {
filter: ['strong'],
replacement: function (content, node, options) {
if (!content.trim()) return ''
return '[#'+ options.strongDelimiter + ']'+ content + '[#'+ options.strongDelimiter + '終わり]'
}
}
rules.bold = {
filter: ['b'],
replacement: function (content, node, options) {
if (!content.trim()) return ''
return '[#'+ options.boldDelimiter + ']'+ content + '[#'+ options.boldDelimiter + '終わり]'
}
}
rules.underline = {
filter: ['u'],
replacement: function (content, node, options) {
if (!content.trim()) return ''
return '[#左に傍線]'+ content + '[#左に傍線終わり]'
}
}
rules.del = {
filter: ['del'],
replacement: function (content, node, options) {
if (!content.trim()) return ''
return '[#取消線]'+ content + '[#取消線終わり]'
}
}
rules.sup = {
filter: ['sup'],
replacement: function (content, node, options) {
if (!content.trim()) return ''
return '[#上付き小文字]'+ content + '[#上付き小文字終わり]'
}
}
rules.sub = {
filter: ['sub'],
replacement: function (content, node, options) {
if (!content.trim()) return ''
return '[#下付き小文字]'+ content + '[#下付き小文字終わり]'
}
}
htmlエディタからhtmlを取得して青空文庫に変換するとエディタになる。
turndownを改造するためのforkしたがその場合のライセンス表記ってどうするんだろうか。マークダウンから青空文庫に変更して名前をhtml2aozoraとしたので、