-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34960fb
commit b4b6c4d
Showing
7 changed files
with
249 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{% extends "!base.html" %} | ||
{% block scripts %} | ||
{{ super() }} | ||
{% include "../../../../docs/resources/slint-docs-preview.html" %} | ||
{% include "../../../../docs/resources/slint-docs-highlight.html" %} | ||
{% include "../../../../docs/src/utils/slint-docs-preview.html" %} | ||
{% include "../../../../docs/src/utils/slint-docs-highlight.html" %} | ||
{% endblock %} |
This file contains 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 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 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,121 @@ | ||
<!-- | ||
This file is used to add syntax highlighting of the `.slint` snippets in the generated rustdoc, sphinx and mdbook documentation. | ||
It can be injected via the `--html-in-header slint-docs-highlight.html` option of rustdoc, is included via _templates/layout.html | ||
in sphinx and via head.hbs in mdbook. | ||
--> | ||
<link rel="stylesheet" href="https://slint.dev/resources/highlightjs/11.0.1/default.min.css"> | ||
<script src="https://slint.dev/resources/highlightjs/11.0.1/highlight.min.js"></script> | ||
<script> | ||
function highlight_slint(hljs) { | ||
const KEYWORDS = { | ||
keyword: | ||
"animate callback changed component enum export for function global if import in in-out inherits out parent private property public pure root self states struct", | ||
literal: "false true", | ||
built_in: | ||
"ArcTo Clip Close Colors CubicTo Flickable FocusScope GridLayout HorizontalLayout Image LineTo Math MoveTo Path PopupWindow QuadraticTo Rectangle Row Text TextInput TouchArea VerticalLayout Window animation-tick debug", | ||
type: "bool duration easing float int length logical-length relative-font-size string", | ||
}; | ||
|
||
return { | ||
name: 'slint', | ||
case_insensitive: false, | ||
keywords: KEYWORDS, | ||
contains: [ | ||
hljs.QUOTE_STRING_MODE, | ||
hljs.C_LINE_COMMENT_MODE, | ||
hljs.C_BLOCK_COMMENT_MODE, | ||
hljs.COMMENT('/\\*', '\\*/', { | ||
contains: ['self'] | ||
}), | ||
{ | ||
className: 'number', | ||
begin: '\\b\\d+(\\.\\d+)?(\\w+)?', | ||
relevance: 0 | ||
}, | ||
{ | ||
className: 'title', | ||
begin: '\\b[_a-zA-Z][_\\-a-zA-Z0-9]* *:=', | ||
}, | ||
{ | ||
className: 'symbol', | ||
begin: '\\b[_a-zA-Z][_\\-a-zA-Z0-9]*(:| *=>)', | ||
}, | ||
{ | ||
className: 'built_in', | ||
begin: '\\b[_a-zA-Z][_\\-a-zA-Z0-9]*!', | ||
}, | ||
], | ||
illegal: /@/ | ||
}; | ||
}; | ||
|
||
// Tags used in fenced code blocks | ||
for (let tag of ["slint", "slint,no-preview", "slint,no-auto-preview", "slint,ignore"]) { | ||
hljs.registerLanguage(tag, highlight_slint); | ||
} | ||
|
||
|
||
window.addEventListener("DOMContentLoaded", () => { | ||
const rustDoc = document.querySelector('meta[name="generator"]')?.content == "rustdoc"; | ||
if (rustDoc) { | ||
// Only highlight .slint blocks, leave the others to rustdoc | ||
for (slintBlock of document.querySelectorAll("[class*=language-slint]")) { | ||
hljs.highlightElement(slintBlock) | ||
} | ||
|
||
// Some of the rustdoc selectors require the pre element to have the rust class | ||
for (codeBlock of document.querySelectorAll(".language-slint.hljs")) { | ||
codeBlock.classList.add("rust"); | ||
codeBlock.classList.remove("hljs"); | ||
} | ||
|
||
// Change the hljs generated classes to the rustdoc | ||
// ones, so that the highlighting adjusts to the theme correctly. | ||
const highlightJSToRustDoc = [ | ||
["comment", "comment"], | ||
["number", "number"], | ||
["symbol", "struct"], // width: | ||
["keyword", "kw"], | ||
["built_in", "primitive"], | ||
["string", "string"], | ||
["title", "fnname"], // Foo := | ||
["type", "type"] | ||
]; | ||
|
||
for ([hljs_class, rustdoc_class] of highlightJSToRustDoc) { | ||
for (titleElement of document.querySelectorAll(`.hljs-${hljs_class}`)) { | ||
titleElement.classList.remove(`hljs-${hljs_class}`); | ||
titleElement.classList.add(rustdoc_class); | ||
} | ||
} | ||
} else { | ||
// For use with the mdbook Tutorial | ||
hljs.highlightAll(); | ||
|
||
// The Sphinx/my_st generated HTML for code blocks doesn't use <code> tags, so highlight.js' | ||
// default selector "pre code" doesn't match. Let's do it by hand: | ||
for (block of document.querySelectorAll("div[class*=highlight-slint] div.highlight pre")) { | ||
hljs.highlightElement(block) | ||
} | ||
} | ||
|
||
// Fix up links that sphinx handles but for rustdoc we need to resolve manually: | ||
{ | ||
let target_url = null; | ||
if (document.location.hostname == "snapshots.slint.dev") { | ||
target_url = `https://snapshots.slint.dev/master/docs/slint/`; | ||
} else { | ||
let version = document.querySelector("span.version"); | ||
if (version !== null) { | ||
target_url = `https://slint.dev/releases/${version.innerText}/docs/slint/`; | ||
} | ||
} | ||
|
||
if (target_url !== null) { | ||
for (let link of document.querySelectorAll("a[href^='slint-reference:']")) { | ||
link.href = link.href.replace("slint-reference:", target_url); | ||
} | ||
} | ||
} | ||
}); | ||
</script> |
This file contains 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,121 @@ | ||
<!-- | ||
This file is used to add preview of the `.slint` snippets in the generated rustdoc documentation. | ||
It can be injected via the `--html-in-header slint-docs-preview.html` option of rustdoc. | ||
--> | ||
<script type="module"> | ||
"use strict"; | ||
//import * as slint from 'https://releases.slint.dev/0.x.0/wasm-interpreter/slint_wasm_interpreter.js'; | ||
//const editor_url = "https://releases.slint.dev/0.x.0/editor/"; | ||
import * as slint from 'https://snapshots.slint.dev/master/wasm-interpreter/slint_wasm_interpreter.js'; | ||
const editor_url = "https://snapshots.slint.dev/master/editor/"; | ||
// keep them alive | ||
var all_instances = new Array; | ||
|
||
async function render_or_error(source, div) { | ||
let canvas_id = 'canvas_' + Math.random().toString(36).substr(2, 9); | ||
let canvas = document.createElement("canvas"); | ||
canvas.id = canvas_id; | ||
div.appendChild(canvas); | ||
|
||
let { component, error_string } = await slint.compile_from_string(source, ""); | ||
if (error_string != "") { | ||
var text = document.createTextNode(error_string); | ||
var p = document.createElement('pre'); | ||
p.appendChild(text); | ||
div.innerHTML = "<pre style='color: red; background-color:#fee; margin:0'>" + p.innerHTML + "</pre>"; | ||
} | ||
if (component !== undefined) { | ||
let instance = await component.create(canvas_id); | ||
await instance.show(); | ||
all_instances.push(instance); | ||
} | ||
} | ||
|
||
async function create_preview(element, source_code) { | ||
// Style the preview such that a flexbox lays out the source code box | ||
// (which should take up any spare space), followed by the preview canvas. | ||
// The latter may wrap into a new row on mobile. The edit/play buttons are | ||
// placed last by flexbox order attribute. | ||
let div = document.createElement("div"); | ||
let sourceCodeBox = element.firstElementChild; | ||
element.style = "display: flex; flex-wrap: wrap;"; | ||
sourceCodeBox.style = "flex-grow: 2"; | ||
element.append(div); | ||
await render_or_error(source_code, div); | ||
} | ||
|
||
function should_show_automatic_preview(element) { | ||
// The `no-auto-preview` doesn't map directly to a dedicated class but it's mangled differently | ||
// between rustdoc and sphinx, so match fuzzy on the entire class list: | ||
return !element.className.includes("no-auto-preview"); | ||
} | ||
|
||
async function create_click_to_play_and_edit_buttons(element) { | ||
let source = element.innerText; | ||
|
||
let link_section = document.createElement("div"); | ||
// Ensure the link section is always last and on a row of its own in the flexbox. | ||
link_section.style = "order: 100; flex-basis: 100%;"; | ||
element.append(link_section); | ||
|
||
let button_style = "text-decoration: none;" | ||
|
||
let edit_button = document.createElement("a"); | ||
edit_button.style = button_style; | ||
edit_button.href = `${editor_url}?snippet=${encodeURIComponent(source)}`; | ||
edit_button.target = "_blank"; | ||
edit_button.innerText = "Edit 📝"; | ||
link_section.append(edit_button); | ||
|
||
if (should_show_automatic_preview(element)) { | ||
create_preview(element, source); | ||
} else { | ||
let play_button = document.createElement("a"); | ||
play_button.style = button_style; | ||
play_button.innerText = "Preview ▶️"; | ||
play_button.onclick = async () => { | ||
play_button.remove(); | ||
create_preview(element, source); | ||
}; | ||
|
||
link_section.prepend(play_button); | ||
} | ||
} | ||
|
||
async function run() { | ||
await slint.default(); | ||
|
||
try { | ||
slint.run_event_loop(); | ||
// this will trigger a JS exception, so this line will never be reached! | ||
} catch (e) { | ||
// The winit event loop, when targeting wasm, throws a JavaScript exception to break out of | ||
// Rust without running any destructors. Don't rethrow the exception but swallow it, as | ||
// this is no error and we truly want to resolve the promise of this function by returning | ||
// the model markers. | ||
} | ||
|
||
let selector = ["code.language-slint", ".rustdoc pre.language-slint", "div.highlight-slint div.highlight", "div.highlight-slint\\,no-auto-preview div.highlight"] | ||
.map((sel) => `${sel}:not([class*=slint\\,ignore]):not([class*=slint\\,no-preview])`).join(","); | ||
var elements = document.querySelectorAll(selector); | ||
for (var i = 0; i < elements.length; ++i) { | ||
await create_click_to_play_and_edit_buttons(elements[i]); | ||
} | ||
} | ||
run(); | ||
|
||
// Included markdown files may have links to other markdown files, which may not have been | ||
// resolved by rustdoc. This helper locates such links and resolves them, assuming that each | ||
// .md file gets its own sub-directory with an index.html. | ||
function fix_markdown_links() { | ||
for (let anchor of document.querySelectorAll('a[href$=".md"], a[href*=".md#"]')) { | ||
let url = new URL(anchor.href); | ||
let dir_separator = Math.max(url.pathname.lastIndexOf("/"), 0); | ||
let base_name = url.pathname.slice(dir_separator + 1, -3); | ||
let base_path = url.pathname.slice(0, dir_separator); | ||
url.pathname = base_path + "/../" + base_name + "/index.html"; | ||
anchor.setAttribute("href", url); | ||
} | ||
} | ||
fix_markdown_links() | ||
</script> |