Skip to content

Commit

Permalink
Fix cpp test
Browse files Browse the repository at this point in the history
  • Loading branch information
NigelBreslaw committed Nov 14, 2024
1 parent 34960fb commit b4b6c4d
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
# Allow deprecated warning because we are using nightly and some things might be deprecated in nightly
# for which the stable alternative is not yet available.
RUSTFLAGS: -D warnings -W deprecated
RUSTDOCFLAGS: --html-in-header=/home/runner/work/slint/slint/docs/resources/slint-docs-highlight.html -D warnings -W deprecated
RUSTDOCFLAGS: --html-in-header=/home/runner/work/slint/slint/docs/src/utils/slint-docs-highlight.html -D warnings -W deprecated
SLINT_NO_QT: 1
CARGO_INCREMENTAL: false
RELEASE_INPUT: ${{ inputs.release }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_new_docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
# Allow deprecated warning because we are using nightly and some things might be deprecated in nightly
# for which the stable alternative is not yet available.
RUSTFLAGS: -D warnings -W deprecated
RUSTDOCFLAGS: --html-in-header=/home/runner/work/slint/slint/docs/resources/slint-docs-highlight.html -D warnings -W deprecated
RUSTDOCFLAGS: --html-in-header=/home/runner/work/slint/slint/docs/src/utils/slint-docs-highlight.html -D warnings -W deprecated
SLINT_NO_QT: 1
CARGO_INCREMENTAL: false
RELEASE_INPUT: ${{ inputs.release }}
Expand Down
4 changes: 2 additions & 2 deletions api/cpp/docs/_templates/base.html
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 %}
4 changes: 2 additions & 2 deletions api/rs/slint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ i-slint-backend-qt = { workspace = true, features = [ "enable" ], optional = tru
[package.metadata.docs.rs]
rustdoc-args = [
# "--html-in-header",
# "docs/resources/slint-docs-preview.html",
# "docs/src/utils/slint-docs-preview.html",
"--html-in-header",
"docs/resources/slint-docs-highlight.html",
"docs/src/utils/slint-docs-highlight.html",
]
features = ["document-features", "log", "gettext", "renderer-software", "renderer-femtovg", "raw-window-handle-06"]
2 changes: 1 addition & 1 deletion docs/archive/building.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ cargo xtask slintdocs --show-warnings
Run the following command to generate the documentation using rustdoc in the `target/doc/` sub-folder:

```sh
RUSTDOCFLAGS="--html-in-header=$PWD/docs/resources/slint-docs-preview.html --html-in-header=$PWD/docs/resources/slint-docs-highlight.html" cargo doc --no-deps --features slint/document-features,slint/log
RUSTDOCFLAGS="--html-in-header=$PWD/docs/src/utils/slint-docs-preview.html --html-in-header=$PWD/docs/src/utils/slint-docs-highlight.html" cargo doc --no-deps --features slint/document-features,slint/log
```

Note: `--html-in-header` arguments passed to rustdoc via `RUSTDOCFLAGS` are used to enable syntax highlighting and live-preview for Slint example snippets.
Expand Down
121 changes: 121 additions & 0 deletions docs/src/utils/slint-docs-highlight.html
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>
121 changes: 121 additions & 0 deletions docs/src/utils/slint-docs-preview.html
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>

0 comments on commit b4b6c4d

Please sign in to comment.