Skip to content

Commit 45994f1

Browse files
committed
[mod] base-uri handling
1 parent 66a5783 commit 45994f1

File tree

13 files changed

+195
-144
lines changed

13 files changed

+195
-144
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 0.1.3 (2024-02-10)
2+
* change status text order
3+
* improve base-uri handling
4+
* add `.xqbk` as prefered file extension.
5+
# 0.1.2 (2024-01-22)
6+
* set base-uri
7+
* statusbar text
18
# 0.1.1 (2024-01-10)
29
* Add configuration and statusbar
310
# 0.0.18

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ Settings
1111
### create a notebook
1212
### add XQuery cells
1313
Adding a code cell defaults to type `xquery`. `Javascript` cells are also supported.
14-
### prolog cells
14+
### header cells
1515
Before executing a XQuery cell, preceding XQuery cells are examined for content starting with '(:<:)' The first such cell, if any, found searching towards the first cell is prefixed to the current cell before execution.
16+
If no `declare base-uri` is present in the header then the notebook url is prepended to the code.
1617

1718
## Notes
1819

package-lock.json

Lines changed: 120 additions & 106 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "quobook",
3-
"displayName": "Quodatum: XQuery notebooks",
4-
"version": "0.1.1",
3+
"displayName": "Quodatum: XQuery notebooks for BaseX",
4+
"version": "0.1.3",
55
"description": "Notebook supporting XQuery cells. The XQuery cells are evaluated via a socket connection to a BaseX server.",
66
"preview": true,
77
"publisher": "quodatum",
@@ -101,6 +101,9 @@
101101
"type": "quobook",
102102
"displayName": "XQuery Notebook",
103103
"selector": [
104+
{
105+
"filenamePattern": "*.xqbk"
106+
},
104107
{
105108
"filenamePattern": "*.xq-notebook"
106109
}

samples/base-uri.xqbk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"kind":2,"language":"xquery","value":"static-base-uri()"}]}

samples/book-to-bxs.xq

Whitespace-only changes.

samples/export.xq-notebook

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"kind":2,"language":"xquery","value":"let $cells:=json:doc(\"C:\\Users\\mrwhe\\git\\quodatum\\xquery-notebook\\samples\\test.xq-notebook\",\r\nmap{\"format\":\"xquery\"})?cells=>array:flatten()\r\n\r\nreturn $cells[?language=\"xquery\"]"},{"kind":2,"language":"xquery","value":""}]}

src/common/cell.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import { match } from 'assert';
2-
import * as vscode from 'vscode';
1+
32
import { NotebookCell, NotebookCellKind } from 'vscode';
43

5-
// 1st XQuery cell with special marker
6-
export function prologCell(cell: NotebookCell) :string {
7-
for (const c of cell.notebook.getCells()) {
8-
if (c.index !== cell.index
9-
&& NotebookCellKind.Code === c.kind
10-
&& "xquery" === c.document.languageId) {
11-
const line= c.document.lineAt(0).text;
12-
if(line.startsWith("(:<:)")) return c.document.getText();
13-
}
4+
// 1st XQuery cell before with special marker
5+
export function findHeader(cell: NotebookCell): string | undefined {
6+
let ci = cell.index;
7+
let header: string | undefined;
8+
do {
9+
const c = cell.notebook.cellAt(ci--);
10+
header = getHeader(c);
11+
if (header) return header;
12+
}
13+
14+
while (ci > 0);
15+
}
16+
// text of cell if "header" else undefined
17+
function getHeader(cell: NotebookCell): string | undefined {
18+
if (NotebookCellKind.Code === cell.kind
19+
&& "xquery" === cell.document.languageId) {
20+
const line = cell.document.lineAt(0).text;
21+
if (line.startsWith("(:<:)")) return cell.document.getText();
1422
}
15-
return "";
1623
}

src/common/configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ export class Configuration {
4949
workspace.getConfiguration(ExtensionTopLevelSection).update(section,value);
5050
}
5151
static get summary():string{
52-
return `${Configuration.hostname}:${Configuration.port}@${Configuration.user}`;
52+
return `${Configuration.user}@${Configuration.hostname}:${Configuration.port}`;
5353
}
5454
}

src/controller.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import * as cellprovider from './languages/cellprovider';
3-
import { prologCell } from './common';
3+
import { findHeader } from './common';
44

55
function output(execution: vscode.NotebookCellExecution, items: any[]) {
66
execution.replaceOutput([new vscode.NotebookCellOutput(items)]);
@@ -45,14 +45,15 @@ export class XQueryKernel {
4545
execution.start(Date.now());
4646

4747
try {
48-
const code =getCode(cell);
48+
const code = getCode(cell);
4949
const result: string[] = await provider.eval(code);
5050
// eslint-disable-next-line prefer-const
5151
let text = asHtml(result);
5252
/* result.forEach(element => { text+=element; }); */
5353
output(execution, [
54-
vscode.NotebookCellOutputItem.text(text, 'text/html'),
55-
vscode.NotebookCellOutputItem.json(result)]);
54+
vscode.NotebookCellOutputItem.json(result)
55+
, vscode.NotebookCellOutputItem.text(text, 'text/html')
56+
]);
5657

5758
execution.end(true, Date.now());
5859
} catch (err: any) {
@@ -65,10 +66,17 @@ export class XQueryKernel {
6566
}
6667

6768
function getCode(cell: vscode.NotebookCell): string {
69+
const base = `declare base-uri "${cell.document.fileName}";`;
70+
const cellText = cell.document.getText();
71+
const header = findHeader(cell);
72+
if (header) {
73+
const hasBase = header.includes('declare base-uri ');
74+
return (hasBase ? "" : base) + header + cellText;
75+
} else {
76+
const hasBase = cellText.includes('declare base-uri ');
77+
return (hasBase ? "" : base) + cellText; + cellText;
78+
}
6879

69-
const code = prologCell(cell)+ cell.document.getText();
70-
71-
return code;
7280
}
7381

7482
function formatResult(item: string): string {

0 commit comments

Comments
 (0)