用 Node.js 編寫的 LeanPub 編譯和清理工具
LeanPub 是一個很棒的出版平台,但我們經常需要與參與圖書製作的其他團隊成員溝通,例如內容和文案編輯。在這種情況下,我知道 Guy Kawasaki 是對的——當我們在他的書 APE:作者、出版商、企業家中建議時——作者必須 使用 MS Word!
問題在於 LeanPub 使用多文件書籍格式和特殊的降價標記(這會使標記的應用感到困惑)。我正在完成一個新版本的 Rapid Prototyping with JS 書,之前將 txt 文件交給編輯的經驗很差。幸運的是,Node.js 提供了幫助!通過一些嘗試和錯誤嘗試,我發現這個工作流程最適合將 LeanPub 手稿轉換為一個 MS Word 文件(用於編輯和其他需要):
- 使用 Node.js 運行 leanpub-tool.js (node leanpub-tool.js)
- 在 Marked 應用中打開文件並轉換為 HTML
- 在 MS Word 中打開 HTML 並開始使用。
也可以在 https://gist.github.com/azat-co/5674684 上找到leanpub-tool.js 的完整代碼:
var fs = require('fs');
//change these to YOUR filenames
//probably we can read it from Book.txt but I was too lazy to implement it :-) and somebody might want to compile only sertain parts of the book
var book =[
"frontmatter.txt",
"mainmatter.txt",
"part1.txt",
"chapter1.txt",
"part2.txt",
"chapter2.txt",
"backmatter.txt",
"acknowledgment.txt"
];
var sanitizeText = [
'{frontmatter}',
'{backmatter}',
'{mainmatter}',
"I>## Note",
"T>## Tip",
"W>## Warning",
'{lang="javascript"}',
'{:lang="javascript"}',
'{lang="css"}',
'{:lang="css"}',
'{lang="json"}',
'{lang="ruby"}',
'{lang="php"}',
'{lang="text"}',
'{lang="bash"}',
'{lang="html"}',
"I>",
"T>",
"W>"
]
var str = '';
//read files
book.forEach(function(chapter){
str += fs.readFileSync(chapter,'utf8');
})
//sanitize LeanPub specific Markdown tags
sanitizeText.forEach(function(text){
// console.log(text)
//this loops through while there is not matches
while (new RegExp (text).test(str)) {
str = str.replace(text,'','gm')
};
})
//write output to a file
fs.writeFileSync('leanpub-tool.txt',str);