-
Notifications
You must be signed in to change notification settings - Fork 906
/
bookModel.ts
255 lines (231 loc) · 8.67 KB
/
bookModel.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as yaml from 'js-yaml';
import { BookTreeItem, BookTreeItemType } from './bookTreeItem';
import * as path from 'path';
import * as fileServices from 'fs';
import * as fs from 'fs-extra';
import * as loc from '../common/localizedConstants';
import { IJupyterBookToc, IJupyterBookSection } from '../contracts/content';
import { ApiWrapper } from '../common/apiWrapper';
const fsPromises = fileServices.promises;
export class BookModel {
private _bookItems: BookTreeItem[];
private _allNotebooks = new Map<string, BookTreeItem>();
private _tableOfContentsPath: string;
private _errorMessage: string;
private apiWrapper: ApiWrapper = new ApiWrapper();
constructor(
public readonly bookPath: string,
public readonly openAsUntitled: boolean,
public readonly isNotebook: boolean,
private _extensionContext: vscode.ExtensionContext) {
this._bookItems = [];
}
public async initializeContents(): Promise<void> {
this._bookItems = [];
this._allNotebooks = new Map<string, BookTreeItem>();
if (this.isNotebook) {
this.readNotebook();
} else {
await this.loadTableOfContentFiles(this.bookPath);
await this.readBooks();
}
}
public getAllNotebooks(): Map<string, BookTreeItem> {
return this._allNotebooks;
}
public getNotebook(uri: string): BookTreeItem | undefined {
return this._allNotebooks.get(this.openAsUntitled ? path.basename(uri) : uri);
}
public async loadTableOfContentFiles(folderPath: string): Promise<void> {
if (this.isNotebook) {
return;
}
let tableOfContentsPath: string = path.posix.join(folderPath, '_data', 'toc.yml');
if (await fs.pathExists(tableOfContentsPath)) {
this._tableOfContentsPath = tableOfContentsPath;
vscode.commands.executeCommand('setContext', 'bookOpened', true);
} else {
this._errorMessage = loc.missingTocError;
throw new Error(loc.missingTocError);
}
}
public readNotebook(): BookTreeItem {
if (!this.isNotebook) {
return undefined;
}
let pathDetails = path.parse(this.bookPath);
let notebookItem = new BookTreeItem({
title: pathDetails.name,
contentPath: this.bookPath,
root: pathDetails.dir,
tableOfContents: { sections: undefined },
page: { sections: undefined },
type: BookTreeItemType.Notebook,
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Expanded,
isUntitled: this.openAsUntitled,
},
{
light: this._extensionContext.asAbsolutePath('resources/light/notebook.svg'),
dark: this._extensionContext.asAbsolutePath('resources/dark/notebook_inverse.svg')
}
);
this._bookItems.push(notebookItem);
if (this.openAsUntitled && !this._allNotebooks.get(pathDetails.base)) {
this._allNotebooks.set(pathDetails.base, notebookItem);
} else {
// convert to URI to avoid casing issue with drive letters when getting navigation links
let uriToNotebook: vscode.Uri = vscode.Uri.file(this.bookPath);
if (!this._allNotebooks.get(uriToNotebook.fsPath)) {
this._allNotebooks.set(uriToNotebook.fsPath, notebookItem);
}
}
return notebookItem;
}
public async readBooks(): Promise<BookTreeItem[]> {
if (this.isNotebook) {
return undefined;
}
if (this._tableOfContentsPath) {
let root: string = path.dirname(path.dirname(this._tableOfContentsPath));
try {
let fileContents = await fsPromises.readFile(path.join(root, '_config.yml'), 'utf-8');
const config = yaml.safeLoad(fileContents.toString());
fileContents = await fsPromises.readFile(this._tableOfContentsPath, 'utf-8');
const tableOfContents: any = yaml.safeLoad(fileContents.toString());
let book: BookTreeItem = new BookTreeItem({
title: config.title,
contentPath: this._tableOfContentsPath,
root: root,
tableOfContents: { sections: this.parseJupyterSections(tableOfContents) },
page: tableOfContents,
type: BookTreeItemType.Book,
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Expanded,
isUntitled: this.openAsUntitled,
},
{
light: this._extensionContext.asAbsolutePath('resources/light/book.svg'),
dark: this._extensionContext.asAbsolutePath('resources/dark/book_inverse.svg')
}
);
this._bookItems.push(book);
} catch (e) {
this._errorMessage = loc.readBookError(this.bookPath, e instanceof Error ? e.message : e);
this.apiWrapper.showErrorMessage(this._errorMessage);
}
}
return this._bookItems;
}
public get bookItems(): BookTreeItem[] {
return this._bookItems;
}
public async getSections(tableOfContents: IJupyterBookToc, sections: IJupyterBookSection[], root: string): Promise<BookTreeItem[]> {
let notebooks: BookTreeItem[] = [];
for (let i = 0; i < sections.length; i++) {
if (sections[i].url) {
if (sections[i].external) {
let externalLink: BookTreeItem = new BookTreeItem({
title: sections[i].title,
contentPath: undefined,
root: root,
tableOfContents: tableOfContents,
page: sections[i],
type: BookTreeItemType.ExternalLink,
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
isUntitled: this.openAsUntitled
},
{
light: this._extensionContext.asAbsolutePath('resources/light/link.svg'),
dark: this._extensionContext.asAbsolutePath('resources/dark/link_inverse.svg')
}
);
notebooks.push(externalLink);
} else {
let pathToNotebook = path.join(root, 'content', sections[i].url.concat('.ipynb'));
let pathToMarkdown = path.join(root, 'content', sections[i].url.concat('.md'));
// Note: Currently, if there is an ipynb and a md file with the same name, Jupyter Books only shows the notebook.
// Following Jupyter Books behavior for now
if (await fs.pathExists(pathToNotebook)) {
let notebook = new BookTreeItem({
title: sections[i].title,
contentPath: pathToNotebook,
root: root,
tableOfContents: tableOfContents,
page: sections[i],
type: BookTreeItemType.Notebook,
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
isUntitled: this.openAsUntitled
},
{
light: this._extensionContext.asAbsolutePath('resources/light/notebook.svg'),
dark: this._extensionContext.asAbsolutePath('resources/dark/notebook_inverse.svg')
}
);
if (this.openAsUntitled) {
if (!this._allNotebooks.get(path.basename(pathToNotebook))) {
this._allNotebooks.set(path.basename(pathToNotebook), notebook);
}
notebooks.push(notebook);
} else {
// convert to URI to avoid casing issue with drive letters when getting navigation links
let uriToNotebook: vscode.Uri = vscode.Uri.file(pathToNotebook);
if (!this._allNotebooks.get(uriToNotebook.fsPath)) {
this._allNotebooks.set(uriToNotebook.fsPath, notebook);
}
notebooks.push(notebook);
}
} else if (await fs.pathExists(pathToMarkdown)) {
let markdown: BookTreeItem = new BookTreeItem({
title: sections[i].title,
contentPath: pathToMarkdown,
root: root,
tableOfContents: tableOfContents,
page: sections[i],
type: BookTreeItemType.Markdown,
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
isUntitled: this.openAsUntitled
},
{
light: this._extensionContext.asAbsolutePath('resources/light/markdown.svg'),
dark: this._extensionContext.asAbsolutePath('resources/dark/markdown_inverse.svg')
}
);
notebooks.push(markdown);
} else {
this._errorMessage = loc.missingFileError(sections[i].title);
this.apiWrapper.showErrorMessage(this._errorMessage);
}
}
} else {
// TODO: search functionality (#6160)
}
}
return notebooks;
}
/**
* Recursively parses out a section of a Jupyter Book.
* @param section The input data to parse
*/
private parseJupyterSections(section: any[]): IJupyterBookSection[] {
try {
return section.reduce((acc, val) => Array.isArray(val.sections) ?
acc.concat(val).concat(this.parseJupyterSections(val.sections)) : acc.concat(val), []);
} catch (e) {
this._errorMessage = loc.invalidTocFileError();
if (section.length > 0) {
this._errorMessage = loc.invalidTocError(section[0].title);
}
throw this._errorMessage;
}
}
public get tableOfContentsPath(): string {
return this._tableOfContentsPath;
}
public get errorMessage(): string {
return this._errorMessage;
}
}