code-to-essay
v1.0.0
Published
The library essay is a code generator from a readme file but this library is a reverse operation.
Downloads
4
Maintainers
Readme
code-to-essay
The library essay is a code generator from a readme file but this library is a reverse operation.
Inspiration
I used eassy
and made a simple project that can generate many code files of javascript (ES7). That can create a good document because you can update code and docs in the same time. I would like to create a converter from code files structure into readme. It will be also created by essay
after convert them.
- essay: https://github.com/dtinth/essay
Getting started
- Install this library
npm i -g code-to-essay
- Write a pathname with prefix
c2e:
in your README.md
c2e:src/main.js
- Convert them with command
c2e
- Setting your project follow
essay
format (https://github.com/dtinth/essay)
Now, the project could be generated by essay
.
Implementation
main.js
will start first when you call c2e build
.
// main.js
#!/usr/bin/env node
import { getTextFromFile, writeFileFromText } from './file'
import { convertLinesToContexts } from './converter'
const README = getTextFromFile(`./README.md`)
const lines = README.split('\n')
const newContexts = convertLinesToContexts(lines)
const newDocs = newContexts.join('\n')
writeFileFromText(newDocs, './README.md')
converter.js
is a module that contains
isCode
check this line is start withc2e:
that mean This line is code, right ?convertCodeToContext
convert codec2e:
to context (code and block)convertLineToContext
pass line as params and return the correct contextconvertLinesToContexts
convert all lines to contexts (array)
// converter.js
import { getTextFromFile, writeFileFromText } from './file.js'
const isCode = (line) => {
return line.indexOf('c2e:') === 0
}
const convertCodeToContext = (line) => {
const pathname = line.split(':').pop()
const header = `// ${pathname}`
const code = getTextFromFile(pathname)
const end = "\`\`\`"
const start = end + 'js'
const context = [start, header, code, end].join('\n')
return context
}
const convertLineToContext = (line) => {
return isCode(line) ? convertCodeToContext(line) : line
}
export const convertLinesToContexts = (lines) => {
const contexts = lines.map((line) => {
const context = convertLineToContext(line)
return context
})
return contexts
}
file.js
is a module that contains
getTextFromFile
receive pathname and return textwriteFileFromText
receive text and pathname as params and write file
// file.js
import { resolve, join } from 'path'
import { readFileSync, writeFileSync } from 'fs'
const projectPath = resolve()
export const getTextFromFile = (pathname) => {
console.log("%s %s", "Reading", pathname)
return readFileSync(join(projectPath, pathname), 'utf-8')
}
export const writeFileFromText = (text, pathname) => {
console.log("%s %s", "Writing", pathname)
return writeFileSync(join(projectPath, pathname), text, 'utf-8')
}