Task #26 - Created functions for exporting to html and archiving the files
This commit is contained in:
parent
47a4826485
commit
a9bc00f9e1
@ -42,6 +42,10 @@
|
|||||||
background-color: beige;
|
background-color: beige;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.navsec a:hover {
|
||||||
|
color: rgb(207, 169, 43);
|
||||||
|
}
|
||||||
|
|
||||||
.nav-link {
|
.nav-link {
|
||||||
color: beige;
|
color: beige;
|
||||||
}
|
}
|
||||||
@ -53,13 +57,9 @@
|
|||||||
<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="#">Navbar</a>
|
<a class="navbar-brand" href="#">Noded</a>
|
||||||
<nav class="nav nav-pills flex-column">
|
<nav class="nav nav-pills flex-column">
|
||||||
<a class="nav-link active" href="#item-1">Item 1</a>
|
%%SIDEBAR%%
|
||||||
<nav class="nav nav-pills flex-column">
|
|
||||||
<a class="nav-link ml-3 my-1" href="#item-1-1">Item 1-1</a>
|
|
||||||
<a class="nav-link ml-3 my-1 active" href="#item-1-2">Item 1-2</a>
|
|
||||||
</nav>
|
|
||||||
</nav>
|
</nav>
|
||||||
</nav>
|
</nav>
|
||||||
</nav>
|
</nav>
|
||||||
@ -67,12 +67,12 @@
|
|||||||
<div class="col-sm-9 contents">
|
<div class="col-sm-9 contents">
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div data-spy="scroll" data-offset="0">
|
<div data-spy="scroll" data-offset="0">
|
||||||
<h4 id="item-1">Item 1</h4>
|
%%CONTENTS%%
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
@ -36,12 +36,13 @@ class Export extends Controller {
|
|||||||
const Page = this.models.get('api:Page')
|
const Page = this.models.get('api:Page')
|
||||||
const Node = this.models.get('api:Node')
|
const Node = this.models.get('api:Node')
|
||||||
const user = req.user
|
const user = req.user
|
||||||
const page0 = await Page.findOne({ OrgUserId: user._id, ParentId: '0' }) //Imaginary root
|
const page0 = await user.get_root_page() //topmost page
|
||||||
const temp_dir = path.resolve(os.tmpdir(), uuid())
|
const temp_dir = path.resolve(os.tmpdir(), uuid())
|
||||||
|
|
||||||
await fs.mkdir(temp_dir)
|
await fs.mkdir(temp_dir)
|
||||||
this._markdown_recurse(page0, temp_dir)
|
this._markdown_recurse(page0, temp_dir)
|
||||||
|
|
||||||
|
/* 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('zip -r ' + path.resolve(temp_dir, 'archive.zip') + ' ' + path.resolve(temp_dir), (error) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
@ -57,7 +58,6 @@ class Export extends Controller {
|
|||||||
async _markdown_recurse(root, temp_dir) { //recursive function for creating directory and generating markdown pages
|
async _markdown_recurse(root, temp_dir) { //recursive function for creating directory and generating markdown pages
|
||||||
console.log('root', root)
|
console.log('root', root)
|
||||||
const Page = this.models.get('api:Page')
|
const Page = this.models.get('api:Page')
|
||||||
//const child = await root.childPages
|
|
||||||
const child = await Page.find({ UUID: { $in: root.ChildPageIds } })
|
const child = await Page.find({ UUID: { $in: root.ChildPageIds } })
|
||||||
|
|
||||||
for (const page of child) {
|
for (const page of child) {
|
||||||
@ -83,12 +83,28 @@ class Export extends Controller {
|
|||||||
const Page = this.models.get('api:Page')
|
const Page = this.models.get('api:Page')
|
||||||
const Node = this.models.get('api:Node')
|
const Node = this.models.get('api:Node')
|
||||||
const user = req.user
|
const user = req.user
|
||||||
const page0 = await Page.findOne({ OrgUserId: user._id, ParentId: '0' }) //Imaginary root
|
const pages = await Page.find({ OrgUserId: user._id })
|
||||||
const temp_dir = path.resolve(os.tmpdir(), uuid())
|
const mapping = {} //keeps track of html file UUID and file name
|
||||||
|
const page0 = await user.get_root_page() //topmost page
|
||||||
|
|
||||||
|
for (const page of pages) {
|
||||||
|
const html_name = page.Name.replace(/\s/g, '_') + '.html'
|
||||||
|
mapping[page.UUID] = html_name
|
||||||
|
}
|
||||||
|
const sidebar_html = this._generate_sidebar(page0) //generates the sidebar contents
|
||||||
|
|
||||||
|
const temp_dir = path.resolve(os.tmpdir(), uuid()) //make temporary directory
|
||||||
await fs.mkdir(temp_dir)
|
await fs.mkdir(temp_dir)
|
||||||
this._html_recurse(page0, temp_dir)
|
|
||||||
|
|
||||||
|
/* Generates html files from template */
|
||||||
|
for(const page of pages) {
|
||||||
|
const fs = require('fs').promises
|
||||||
|
const contents = await fs.readFile('../assets/export-template.html')
|
||||||
|
const content_html = this._generate_html(page)
|
||||||
|
const page_html = contents.replace('%%SIDEBAR%%', sidebar_html).replace('%%CONTENTS%%', content_html)
|
||||||
|
await fs.writeFile(path.resolve(temp_dir, mapping[page.UUID]), page_html)
|
||||||
|
}
|
||||||
|
/* 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('zip -r ' + path.resolve(temp_dir, 'archive.zip') + ' ' + path.resolve(temp_dir), (error) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
@ -100,25 +116,21 @@ class Export extends Controller {
|
|||||||
return res.sendFile(`${path.resolve(temp_dir, 'archive.zip')}`)
|
return res.sendFile(`${path.resolve(temp_dir, 'archive.zip')}`)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
async _html_recurse(root, temp_dir) { //recursive function for creating directory and generating html pages
|
async _generate_sidebar(root) { //generate sidebar from mapping array and root
|
||||||
console.log('root', root)
|
if (root.ChildPageIds < 1) {
|
||||||
const Page = this.models.get('api:Page')
|
return '<a class="nav-link" href="' + mapping[root.UUID] + '">' + root.Name + '</a>'
|
||||||
//const child = await root.childPages
|
} else {
|
||||||
const child = await Page.find({ UUID: { $in: root.ChildPageIds } })
|
let base = '<nav class="nav nav-pills flex-column">'
|
||||||
|
for (const childrenId of root.ChildPageIds) {
|
||||||
for (const page of child) {
|
base += this._generate_sidebar(await Page.findOne({ UUID: childrenId }))
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
return base
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async _generate_html(page) { //generate html
|
async _generate_html(page) { //generate html page from page and node data
|
||||||
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')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user