Task #26 - Rewrote export as html functions.

This commit is contained in:
QiTao 2020-02-09 03:19:20 -06:00
parent 8714ab79f1
commit f24ecd2aa2
2 changed files with 32 additions and 23 deletions

View File

@ -33,6 +33,7 @@
} }
.nav { .nav {
margin-left: -1rem;
width: 100%; width: 100%;
} }
@ -49,6 +50,9 @@
.nav-link { .nav-link {
color: beige; color: beige;
} }
.contents {
padding: 2em;
}
</style> </style>
</head> </head>
@ -57,11 +61,10 @@
<div class="row"> <div class="row">
<div class="col-sm-3 navsec"> <div class="col-sm-3 navsec">
<nav class="navbar navbar-dark bg-dark flex-column"> <nav class="navbar navbar-dark bg-dark flex-column">
<a class="navbar-brand" href="#">Noded</a> <a class="navbar-brand" href="#"><img src="/logo/Asset+1.svg" width="30" height="30" class="d-inline-block align-top" alt="">Noded</a>
<nav class="nav nav-pills flex-column"> <div class="nav nav-pills flex-column">
%%SIDEBAR%% %%SIDEBAR%%
</nav> </div>
</nav>
</nav> </nav>
</div> </div>
<div class="col-sm-9 contents"> <div class="col-sm-9 contents">

View File

@ -44,7 +44,6 @@ class Export extends Controller {
/* Archive directory */ /* Archive directory */
const exec = require('child_process').exec; const exec = require('child_process').exec;
console.log('zip -9jpr ' + path.resolve(temp_dir, '../archive.zip') + ' ' + path.resolve(temp_dir) + '/')
exec('cd ' + path.resolve(temp_dir) + '; zip -r archive.zip *', (error) => { exec('cd ' + path.resolve(temp_dir) + '; zip -r archive.zip *', (error) => {
if (error) { if (error) {
console.error(`exec error: ${error}`); console.error(`exec error: ${error}`);
@ -93,8 +92,8 @@ class Export extends Controller {
const html_name = page.Name.replace(/\s/g, '_') + '.html' const html_name = page.Name.replace(/\s/g, '_') + '.html'
mapping[page.UUID] = html_name mapping[page.UUID] = html_name
} }
console.log(page0)
const sidebar_html = await this._generate_sidebar(mapping, page0) //generates the sidebar contents const sidebar_html = await this._generate_sidebar(mapping, user) //generates the sidebar contents
const temp_dir = path.resolve(os.tmpdir(), uuid()) //make temporary directory const temp_dir = path.resolve(os.tmpdir(), uuid()) //make temporary directory
await fs.mkdir(temp_dir) await fs.mkdir(temp_dir)
@ -102,16 +101,13 @@ class Export extends Controller {
for (const page of pages) { for (const page of pages) {
const fs = require('fs').promises const fs = require('fs').promises
const contents = (await fs.readFile(path.resolve(this.app.directories.assets, 'export-template.html'))).toString('utf8') const contents = (await fs.readFile(path.resolve(this.app.directories.assets, 'export-template.html'))).toString('utf8')
console.log(page)
//console.log({contents})
const content_html = await this._generate_html(page) const content_html = await this._generate_html(page)
//console.log({content_html, sidebar_html}) const page_html = contents.replace('%%CONTENTS%%', content_html).replace('%%SIDEBAR%%', sidebar_html)
const page_html = contents.replace('%%SIDEBAR%%', sidebar_html).replace('%%CONTENTS%%', content_html)
await fs.writeFile(path.resolve(temp_dir, mapping[page.UUID]), page_html) await fs.writeFile(path.resolve(temp_dir, mapping[page.UUID]), page_html)
} }
/* Archive directory */ /* Archive directory */
const exec = require('child_process').exec; const exec = require('child_process').exec;
exec('zip -r ' + path.resolve(temp_dir, 'archive.zip') + ' ' + path.resolve(temp_dir), (error) => { exec('cd ' + path.resolve(temp_dir) + '; zip -r archive.zip *', (error) => {
if (error) { if (error) {
console.error(`exec error: ${error}`); console.error(`exec error: ${error}`);
return res.error(500); return res.error(500);
@ -120,22 +116,32 @@ class Export extends Controller {
return res.sendFile(`${path.resolve(temp_dir, 'archive.zip')}`) return res.sendFile(`${path.resolve(temp_dir, 'archive.zip')}`)
}) })
} }
async _generate_sidebar(mapping, upper_node) { //generate sidebar from mapping array and upper_node async _generate_sidebar(mapping, user) {// the absolutely dreadful method to create, sidebar
const Page = this.models.get('api:Page') const Page = this.models.get('api:Page')
console.log(upper_node) const Node = this.models.get('api:Node')
console.log(upper_node.ChildPageIds == 'undefined') const virt_root = await user.get_root_page()
if (upper_node.ChildPageIds == 'undefined') { return await this._sidebar_recurse(mapping, virt_root)
return '<a class="nav-link" href="' + mapping[upper_node.UUID] + '">' + upper_node.Name + '</a>'
} else { }
let base = '<nav class="nav nav-pills flex-column">' async _sidebar_recurse(mapping, parent) {
for (const childrenId of upper_node.ChildPageIds) { const Page = this.models.get('api:Page')
base += (await this._generate_sidebar(await Page.findOne({ UUID: childrenId }))) const Node = this.models.get('api:Node')
const children = await Page.find({ ParentId : parent.UUID})
let base = ''
for (const child of children) {
if (child.ChildPageIds.length === 0) {
base += '<a class="nav-link ml-2" href="' + mapping[child.UUID] + '">' + child.Name + '</a>' //mapping[child.UUID] gives filename
} else {
base += '<a class="nav-link ml-2" href="' + mapping[child.UUID] + '">' + child.Name + '</a>'
base += '<div class="nav nav-pills flex-column ml-2">'
base += await this._sidebar_recurse(mapping, child)
base += '</div>'
}
} }
console.log({ base })
return base return base
} }
}
async _generate_html(page) { //generate html page from page and node data async _generate_html(page) { //generate html page from page and node data
const Page = this.models.get('api:Page')
const Node = this.models.get('api:Node') const Node = this.models.get('api:Node')
const nodes = await Node.find({ PageId: page.UUID }) const nodes = await Node.find({ PageId: page.UUID })
return nodes.map(x => { return x.to_html() }).join('\n') return nodes.map(x => { return x.to_html() }).join('\n')