Skip to content

Instantly share code, notes, and snippets.

@rollacaster
Created October 27, 2018 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rollacaster/473c6352122befbd65bdcbeb2e2a887b to your computer and use it in GitHub Desktop.
Save rollacaster/473c6352122befbd65bdcbeb2e2a887b to your computer and use it in GitHub Desktop.
All the things Example
const util = require('util')
const fs = require('fs')
const exec = util.promisify(require('child_process').exec)
const { split, compose, trim, composeK } = require('ramda')
const { createElement } = require('react')
const ReactDOMServer = require('react-dom/server')
const { List } = require('react-virtualized')
const Task = require('data.task')
const c = composeK
const cat = file =>
new Task((reject, resolve) =>
exec(`cat ${file}`)
.then(({ stdout }) => resolve(stdout))
.catch(reject)
)
const VList = rowRenderer => list =>
Task.of(
createElement(
List,
{
rowCount: list.length,
height: 200,
rowHeight: 30,
width: 200,
rowRenderer: rowRenderer(list)
},
null
)
)
const addClass = className => element => ({
...element,
props: {
...element.props,
className: (element.props.className || '') + ' ' + className
}
})
const div = ({ key, style, content }) =>
createElement('div', { key, style }, content)
const rowRenderer = element => list => ({
key,
style,
index
}) => element({ key, style, content: list[index] })
const toArray = text =>
Task.of(
compose(
split('\n'),
trim
)(text)
)
const indexHtml = content =>
`<html lang="en">
<head>
<meta charset="utf-8">
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
<link rel="stylesheet" href="https://unpkg.com/tachyons/css/tachyons.min.css"/>
<title></title>
</head>
<style>
.bg-blue {
background-color: #2e8ccf;
}
.bg-orange {
background-color: #b4881d;
}
</style>
<body class="sans-serif ma2" style="background-color:#194455">
${content}
</body>
</html>`
const genHtml = data =>
Task.of(
compose(
data => fs.writeFileSync('./index.html', data) || data,
indexHtml,
ReactDOMServer.renderToString
)(data)
)
const blueRow = rowRenderer(
compose(
addClass('white'),
addClass('bg-blue'),
addClass('pa2'),
addClass('bb'),
div
)
)
const allTheThings = c(genHtml, VList(blueRow), toArray, cat)
allTheThings('sample.txt').fork(console.error, console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment