Task #26 - Created general layout for exporting to html

This commit is contained in:
QiTao 2020-02-08 12:47:03 -06:00
parent 80d568a520
commit 29a873559b
3 changed files with 62 additions and 1 deletions

View File

@ -79,6 +79,47 @@ class Export extends Controller {
return x.Value.Value
}).join('\n')
}
async html_export(req, res) {
const Page = this.models.get('api:Page')
const Node = this.models.get('api:Node')
const user = req.user
const page0 = await Page.findOne({ OrgUserId: user._id, ParentId: '0' }) //Imaginary root
const temp_dir = path.resolve(os.tmpdir(), uuid())
await fs.mkdir(temp_dir)
this._html_recurse(page0, temp_dir)
const exec = require('child_process').exec;
exec('zip -r ' + path.resolve(temp_dir, 'archive.zip') + ' ' + path.resolve(temp_dir), (error) => {
if (error) {
console.error(`exec error: ${error}`);
return res.error(500);
return;
}
return res.sendFile(`${path.resolve(temp_dir, 'archive.zip')}`)
})
}
async _html_recurse(root, temp_dir) { //recursive function for creating directory and generating html pages
console.log('root', root)
const Page = this.models.get('api:Page')
//const child = await root.childPages
const child = await Page.find({ UUID: { $in: root.ChildPageIds } })
for (const page of child) {
const html = this._generate_html(page)
if (page.ChildPageIds && page.ChildPageIds.length) {
const sub_temp_dir = path.resolve(temp_dir, page.Name.replace(/\s/g, '_'))
await fs.mkdir(sub_temp_dir)
this._html_recurse(page, sub_temp_dir)
}
}
}
async _generate_html(page) { //generate html
const Node = this.models.get('api:Node')
const nodes = await Node.find({ PageId: page.UUID })
return nodes.map(x => {return x.to_html()}).join('\n')
}
}
module.exports = exports = Export

View File

@ -30,6 +30,25 @@ class Node extends Model {
return this.belongs_to_one(Page, "PageId", "_id")
}
to_html() {
switch(this.Type){
case 'paragraph':
return '<p>' + this.Value.Value + '</p>'
case 'header1':
return '<h1>' + this.Value.Value + '</h1>'
case 'header2':
return '<h2>' + this.Value.Value + '</h2>'
case 'header3':
return '<h3>' + this.Value.Value + '</h3>'
case 'header4':
return '<h4>' + this.Value.Value + '</h4>'
case 'block_code':
return '<pre><code>' + this.Value.Value + '</code></pre>'
case 'click_link':
return '<a href="' + this.Value.Value + '">' + this.Value.Value + '</a>'
}
}
update_from_raw(data) {
if ( data.Type ) this.Type = data.Type
if ( data.Value ) this.Value = data.Value

View File

@ -47,7 +47,8 @@ const index = {
'/dash': [ 'controller::Home.welcome' ],
'/login': [ 'middleware::auth:GuestOnly', 'controller::Home.get_login' ],
'/test-json' : [ 'controller::Export.json_export' ],
'/test-markdown' : [ 'controller::Export.markdown_export' ]
'/test-markdown' : [ 'controller::Export.markdown_export' ],
'/test-html' : [ 'controller::Export.html_export' ]
},
/*