-
Notifications
You must be signed in to change notification settings - Fork 4.4k
pdf.html generator for Writerside #4577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { dirname, join } from 'node:path'; | ||
| import { EventEmitter } from 'events'; | ||
|
|
||
| import { DIST_FOLDER } from '../lib/files/index.js'; | ||
| import { appendFile, open, readFile } from 'node:fs/promises'; | ||
| import { processTocToUrls, Result } from './lib.js'; | ||
| import { newTaskExecutor } from '../lib/pool.js'; | ||
|
|
||
| console.time('Data successfully built'); | ||
|
|
||
| const TASK_PATH = import.meta.dirname + '/task'; | ||
|
|
||
| EventEmitter.defaultMaxListeners = 15; | ||
|
|
||
| const DOCS_PATH = join(DIST_FOLDER, 'docs'); | ||
| const TOC_PATH = join(DOCS_PATH, 'HelpTOC.json'); | ||
|
|
||
| const TOC = JSON.parse(await readFile(TOC_PATH, { encoding: 'utf-8' })); | ||
|
|
||
| const nodes = new Map( | ||
| (await processTocToUrls(TOC)) | ||
| .map(id => new URL(id, 'https://kotlinlang.org/docs/')) | ||
| .filter(url => url.hostname === 'kotlinlang.org' && dirname(url.pathname) === '/docs') | ||
| .map(url => url.pathname.slice(1)) | ||
| .map(key => [key, '']) | ||
| ); | ||
|
|
||
| if (nodes.size === 0) throw new Error('No nodes found'); | ||
|
|
||
| async function onItem({ id, html }: Result) { | ||
| nodes.set(id, html); | ||
| } | ||
|
|
||
| const [pool, finish] = newTaskExecutor( | ||
| TASK_PATH, onItem | ||
| ); | ||
|
|
||
| for (const id of nodes.keys()) { | ||
| pool.push(id); | ||
| } | ||
|
|
||
| await finish; | ||
|
|
||
| const pdfFile = await open(join(DOCS_PATH, 'pdf.html'), 'w'); | ||
|
|
||
| for (const html of nodes.values()) { | ||
| await appendFile(pdfFile, html); | ||
| } | ||
|
|
||
| await pdfFile.close(); | ||
|
|
||
| console.timeEnd('Data successfully built'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { join } from 'node:path'; | ||
| import { readFile } from 'node:fs/promises'; | ||
| import { CheerioAPI, load } from 'cheerio'; | ||
|
|
||
| import { FileType } from '../lib/files/type.js'; | ||
| import { ROOT_DIR } from '../lib/files/index.js'; | ||
|
|
||
| export type Result = { | ||
| id: string | ||
| html: string | ||
| } | ||
|
|
||
| export function isHiddenProved(relativePath: string, type?: FileType) { | ||
| return (!type || type === 'Hidden') && ( | ||
| relativePath.startsWith('docs/kotlin-tour-') || | ||
| relativePath === 'docs/multiplatform.html' | ||
| ); | ||
| } | ||
|
|
||
| const TREE_XML_PATH = join(ROOT_DIR + '/docs/kr.tree'); | ||
|
|
||
| let TREE_XML: CheerioAPI | null = null; | ||
|
|
||
| async function readTreeXMLEntities() { | ||
| if (!TREE_XML) | ||
| TREE_XML = load(await readFile(TREE_XML_PATH, { encoding: 'utf-8' }), { | ||
| xml: true | ||
| }); | ||
|
|
||
| return TREE_XML; | ||
| } | ||
|
|
||
| async function getSubtreeItems(id: string) { | ||
| const $tree = await readTreeXMLEntities(); | ||
| const topic = id.replace(/\.html$/, '.md'); | ||
|
|
||
| return $tree(`toc-element[topic="${topic}"] > toc-element[topic]`).toArray().map(node => { | ||
| const file = node?.attribs?.topic?.replace(/\.(md|topic)$/, '.html'); | ||
| if (!file || !isHiddenProved(`docs/${file}`)) return null; | ||
| return file; | ||
| }) | ||
| .filter(Boolean); | ||
| } | ||
|
|
||
| export async function processTocToUrls({ topLevelIds, entities }: { | ||
| entities: { pages: Record<string, { url?: string, pages: string[] }> }, | ||
| topLevelIds: string[] | ||
| }) { | ||
| const result: string[] = []; | ||
| const visited = new Set<string>(); | ||
| const stack = [...topLevelIds.reverse()]; // Reverse to maintain order | ||
| const data = entities.pages; | ||
| while (stack.length) { | ||
| const id = stack.pop(); | ||
|
|
||
| if (visited.has(id)) continue; | ||
| visited.add(id); | ||
|
|
||
| const page = data[id]; | ||
| if (!page) continue; | ||
| const { url, pages } = page; | ||
|
|
||
| if (url) result.push(page.url); | ||
|
|
||
| if (isHiddenProved(`docs/${page.url}`)) { | ||
| const hiddenPages = await getSubtreeItems(page.url); | ||
| for (const id of hiddenPages) { | ||
| result.push(id); | ||
| } | ||
| } | ||
|
|
||
| if (pages?.length) { | ||
| for (let i = page.pages.length - 1; i >= 0; i--) { | ||
| stack.push(page.pages[i]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.