Skip to content
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

Try to have custom renderer with myst. #314

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ build
test.dat
test.npy
test.out
node_modules
*.js.map

papyri/app/asset-manifest.json
papyri/app/index.html
5 changes: 4 additions & 1 deletion examples/scipy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ exclude = [
"scipy.sparse.linalg._expm_multiply:expm_multiply",

# contain ascii diagram
"scipy.stats._continuous_distns:crystalball_gen._pdf"
"scipy.stats._continuous_distns:crystalball_gen._pdf",

# assert len(tsc) in (0, 1), (tsc, data)
"scipy.io.matlab._mio5"
]
exclude_jedi = [
"scipy.linalg._sketches.clarkson_woodruff_transform",
Expand Down
8 changes: 8 additions & 0 deletions frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# attempt to build a JS rendered based on Myst for papyri.

```
$ npm run build
```

Should put the js/css artifacts in ./build/static/

16 changes: 16 additions & 0 deletions frontend/craco.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
webpack: {
configure: {
module: {
rules: [
{
test: /\.m?js$/,
resolve: {
fullySpecified: false,
},
},
],
},
},
},
};
45 changes: 45 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "papyri-frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"mdast-util-from-markdown": "^2.0.0",
"myst-to-react": "^0.5.17",
"prettier": "^3.0.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"unist-util-visit": "^5.0.0",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "craco start",
"build": "BUILD_PATH='../papyri/app/' craco build",
"test": "craco test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@craco/craco": "^7.1.0"
}
}
12 changes: 12 additions & 0 deletions frontend/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<!--
Apparently you need to have a index.html to buils js, I don't quite understand.
-->
</body>
</html>
13 changes: 13 additions & 0 deletions frontend/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
197 changes: 197 additions & 0 deletions frontend/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import { ThemeProvider } from "@myst-theme/providers";
import { MyST, DEFAULT_RENDERERS } from "myst-to-react";

import { fromMarkdown } from "mdast-util-from-markdown";

const Foo = ({ node }) => {
if (!node.children) return <span>{node.value}</span>;
return (
<div className="not-implemented">
<MyST ast={node.children} />
</div>
);
};

const MUnimpl = ({ node }) => {
if (!node.children) return <span>{node.value}</span>;
return (
<div className="not-implemented unimpl">
<MyST ast={node.children} />
</div>
);
};

const Param = ({ node }) => {
return (
<>
<dt>
{node.param}: {node.type_}
</dt>
<dd>
{node.desc.map((sub) => (
<MyST ast={sub} />
))}
</dd>
</>
);
};
const Parameters = ({ node }) => {
return (
<dl>
{node.children.map((item) => (
<MyST ast={item} />
))}
</dl>
);
};

const DefList = ({ node }) => {
return (
<dl>
{node.children.map((item) => (
<>
<dt>
<MyST ast={item.dt} />
</dt>
<dd>
{item.dd.map((sub) => (
<MyST ast={sub} />
))}
</dd>
</>
))}
</dl>
);
};

// render a single parameter in a signature
const ParameterNodeRenderer = ({ node }) => {
if (node.kind === "/") {
return <span>/</span>;
}
let comp = [];
let name = "";
if (node.kind === "VAR_POSITIONAL") {
name = "*";
}
if (node.kind === "VAR_KEYWORD") {
name += "**";
}
name += node.name;

comp = [<span>{name}</span>];

if (node.annotation.type !== "Empty") {
comp.push(<span className="type-ann">{": " + node.annotation.data}</span>);
}
if (node.default.type !== "Empty") {
comp.push(<span className="default-value">{"=" + node.default.data}</span>);
}

return <>{comp}</>;
};

const SignatureRenderer = ({ node }) => {
let prev = "";
let acc = [];
for (let i = 0; i < node.parameters.length; i++) {
const p = node.parameters[i];
if (p.kind !== "POSITIONAL_ONLY" && prev === "POSITIONAL_ONLY") {
acc.push({ kind: "/", type: "ParameterNode" });
}
prev = p.kind;
acc.push(p);
}
return (
<code className="flex my-5 group signature">
{node.kind.indexOf("async") !== -1 ||
node.kind.indexOf("coroutine") !== -1
? "async "
: ""}
def {node.target_name}(
<>
{/* TODO: insert the / from positional only */}
{acc.map((parameter, index, array) => {
if (index + 1 === array.length) {
return <MyST ast={parameter} />;
} else {
return (
<>
<MyST ast={parameter} />
{", "}
</>
);
}
})}
</>
{")"}
<span className="ret-ann">
{"->"} {node.return_annotation.data}
</span>
:
</code>
);
};

const Directive = ({ node }) => {
const dom = node.domain !== null ? ":" + node.domain : "";
const role = node.role !== null ? ":" + node.role + ":" : "";
return (
<>
<code className="not-implemented">
<span>
{dom}
{role}`{node.value}`
</span>
</code>
</>
);
};

const LOC = {
signature: SignatureRenderer,
Directive: Directive,
DefList: DefList,
Parameters: Parameters,
ParameterNode: ParameterNodeRenderer,
Param: Param,
MUnimpl: MUnimpl,
DefaultComponent: Foo,
};
const RENDERERS = { ...DEFAULT_RENDERERS, ...LOC };

function MyComponent({ node }) {
return <MyST ast={node.children} />;
}

//const tree = fromMarkdown("Some *emphasis*, **strong**, and `code`.");
//const mytree = {
// type: "admonition",
// children: [
// { type: "text", value: "myValue" },
// {
// type: "signature",
// value: "Foo",
// children: [{ type: "text", value: "Child" }],
// },
// ],
//};

console.log("Loading X");

const render = (id, tree) => {
const root = ReactDOM.createRoot(document.getElementById(id));
root.render(
<React.StrictMode>
<ThemeProvider renderers={RENDERERS}>
<MyComponent node={tree} />
</ThemeProvider>
</React.StrictMode>,
);
};

window.render = render;
window.fromMarkdown = fromMarkdown;
5 changes: 5 additions & 0 deletions frontend/src/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom';
5 changes: 5 additions & 0 deletions frontend/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
output: {
filename: 'your-custom-name.js', // Set your custom output filename here
},
};
2 changes: 2 additions & 0 deletions papyri/app/static/css/main.e6c13ad2.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions papyri/app/static/js/main.27408fde.js

Large diffs are not rendered by default.

57 changes: 57 additions & 0 deletions papyri/app/static/js/main.27408fde.js.LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*!
Copyright (c) 2018 Jed Watson.
Licensed under the MIT License (MIT), see
http://jedwatson.github.io/classnames
*/

/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */

/**
* @license React
* react-dom.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* @license React
* react-jsx-runtime.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* @license React
* react.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* @license React
* scheduler.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* @license React
* use-sync-external-store-shim.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading