update docs syntax highlighting, remove unsused code

This commit is contained in:
Luke Boswell 2023-03-06 22:20:49 +11:00
parent 424931459d
commit 5e40e580cb
No known key found for this signature in database
GPG key ID: F6DB3C9DB47377B0
14 changed files with 327 additions and 2871 deletions

View file

@ -1,55 +0,0 @@
use peg::error::ParseError;
use roc_ast::ast_error::ASTError;
use roc_module::module_err::ModuleError;
use roc_parse::parser::SyntaxError;
use snafu::Snafu;
#[derive(Debug, Snafu)]
#[snafu(visibility(pub))]
#[allow(clippy::enum_variant_names)]
pub enum DocsError {
WrapASTError {
#[snafu(backtrace)]
source: ASTError,
},
WrapModuleError {
#[snafu(backtrace)]
source: ModuleError,
},
WrapSyntaxError {
msg: String,
},
WrapPegParseError {
source: ParseError<usize>,
},
}
pub type DocsResult<T, E = DocsError> = std::result::Result<T, E>;
impl<'a> From<SyntaxError<'a>> for DocsError {
fn from(syntax_err: SyntaxError) -> Self {
Self::WrapSyntaxError {
msg: format!("{:?}", syntax_err),
}
}
}
impl From<ASTError> for DocsError {
fn from(ast_err: ASTError) -> Self {
Self::WrapASTError { source: ast_err }
}
}
impl From<ModuleError> for DocsError {
fn from(module_err: ModuleError) -> Self {
Self::WrapModuleError { source: module_err }
}
}
impl From<ParseError<usize>> for DocsError {
fn from(peg_parse_err: ParseError<usize>) -> Self {
Self::WrapPegParseError {
source: peg_parse_err,
}
}
}

View file

@ -1,85 +0,0 @@
use roc_code_markup::{markup::nodes::MarkupNode, slow_pool::SlowPool};
// determine appropriate css class for MarkupNode
pub fn mark_node_to_html(mark_node: &MarkupNode, mark_node_pool: &SlowPool, buf: &mut String) {
let mut additional_newlines = 0;
match mark_node {
MarkupNode::Nested {
children_ids,
newlines_at_end,
..
} => {
for &child_id in children_ids {
mark_node_to_html(mark_node_pool.get(child_id), mark_node_pool, buf)
}
additional_newlines = *newlines_at_end;
}
MarkupNode::Text {
content,
syn_high_style,
newlines_at_end,
..
} => {
use roc_code_markup::syntax_highlight::HighlightStyle::*;
let css_class = match syn_high_style {
Operator => "operator",
String => "string",
FunctionName => "function-name",
FunctionArgName => "function-arg-name",
Type => "type",
Bracket => "bracket",
Number => "number",
PackageRelated => "package-related",
Value => "value",
RecordField => "recordfield",
Import => "import",
Provides => "provides",
Blank => "blank",
Comment => "comment",
DocsComment => "docs-comment",
UppercaseIdent => "uppercase-ident",
LowercaseIdent => "lowercase-ident",
Keyword => "keyword-ident",
};
write_html_to_buf(content, css_class, buf);
additional_newlines = *newlines_at_end;
}
MarkupNode::Blank {
newlines_at_end, ..
} => {
let mut content_str = " ".to_string();
for _ in 0..*newlines_at_end {
content_str.push('\n');
}
write_html_to_buf(&content_str, "blank", buf);
additional_newlines = *newlines_at_end;
}
MarkupNode::Indent { .. } => {
let content_str = mark_node.get_content();
write_html_to_buf(&content_str, "indent", buf);
}
}
for _ in 0..additional_newlines {
buf.push('\n')
}
}
fn write_html_to_buf(content: &str, css_class: &'static str, buf: &mut String) {
let opening_tag: String = ["<span class=\"syntax-", css_class, "\">"].concat();
buf.push_str(opening_tag.as_str());
buf.push_str(content);
buf.push_str("</span>");
}

View file

@ -3,13 +3,8 @@
extern crate pulldown_cmark;
extern crate roc_load;
use bumpalo::Bump;
use docs_error::{DocsError, DocsResult};
use html::mark_node_to_html;
use roc_can::scope::Scope;
use roc_code_markup::markup::nodes::MarkupNode;
use roc_code_markup::slow_pool::SlowPool;
use roc_collections::VecSet;
use roc_highlight::highlight_parser::{highlight_defs, highlight_expr};
use roc_load::docs::{DocEntry, TypeAnnotation};
use roc_load::docs::{ModuleDocumentation, RecordField};
use roc_load::{ExecutionMode, LoadConfig, LoadedModule, LoadingProblem, Threading};
@ -21,9 +16,6 @@ use roc_region::all::Region;
use std::fs;
use std::path::{Path, PathBuf};
mod docs_error;
mod html;
const BUILD_DIR: &str = "./generated-docs";
pub fn generate_docs_html(root_file: PathBuf) {
@ -132,47 +124,47 @@ fn page_title(package_name: &str, module_name: &str) -> String {
}
// converts plain-text code to highlighted html
pub fn syntax_highlight_expr(code_str: &str) -> DocsResult<String> {
let trimmed_code_str = code_str.trim_end().trim();
let mut mark_node_pool = SlowPool::default();
// pub fn syntax_highlight_expr(code_str: &str) -> DocsResult<String> {
// let trimmed_code_str = code_str.trim_end().trim();
// let mut mark_node_pool = SlowPool::default();
let mut highlighted_html_str = String::new();
// let mut highlighted_html_str = String::new();
match highlight_expr(trimmed_code_str, &mut mark_node_pool) {
Ok(root_mark_node_id) => {
let root_mark_node = mark_node_pool.get(root_mark_node_id);
mark_node_to_html(root_mark_node, &mark_node_pool, &mut highlighted_html_str);
// match highlight_expr(trimmed_code_str, &mut mark_node_pool) {
// Ok(root_mark_node_id) => {
// let root_mark_node = mark_node_pool.get(root_mark_node_id);
// mark_node_to_html(root_mark_node, &mark_node_pool, &mut highlighted_html_str);
Ok(highlighted_html_str)
}
Err(err) => Err(DocsError::from(err)),
}
}
// Ok(highlighted_html_str)
// }
// Err(err) => Err(DocsError::from(err)),
// }
// }
// converts plain-text code to highlighted html
pub fn syntax_highlight_top_level_defs(code_str: &str) -> DocsResult<String> {
let trimmed_code_str = code_str.trim_end().trim();
// pub fn syntax_highlight_top_level_defs(code_str: &str) -> DocsResult<String> {
// let trimmed_code_str = code_str.trim_end().trim();
let mut mark_node_pool = SlowPool::default();
// let mut mark_node_pool = SlowPool::default();
let mut highlighted_html_str = String::new();
// let mut highlighted_html_str = String::new();
match highlight_defs(trimmed_code_str, &mut mark_node_pool) {
Ok(mark_node_id_vec) => {
let def_mark_nodes: Vec<&MarkupNode> = mark_node_id_vec
.iter()
.map(|mn_id| mark_node_pool.get(*mn_id))
.collect();
// match highlight_defs(trimmed_code_str, &mut mark_node_pool) {
// Ok(mark_node_id_vec) => {
// let def_mark_nodes: Vec<&MarkupNode> = mark_node_id_vec
// .iter()
// .map(|mn_id| mark_node_pool.get(*mn_id))
// .collect();
for mn in def_mark_nodes {
mark_node_to_html(mn, &mark_node_pool, &mut highlighted_html_str)
}
// for mn in def_mark_nodes {
// mark_node_to_html(mn, &mark_node_pool, &mut highlighted_html_str)
// }
Ok(highlighted_html_str)
}
Err(err) => Err(DocsError::from(err)),
}
}
// Ok(highlighted_html_str)
// }
// Err(err) => Err(DocsError::from(err)),
// }
// }
fn render_module_documentation(
module: &ModuleDocumentation,
@ -857,7 +849,8 @@ fn markdown_to_html(
let markdown_options = pulldown_cmark::Options::ENABLE_TABLES;
let mut expecting_code_block = false;
let mut in_code_block = false;
let mut to_highlight = String::new();
let mut docs_parser = vec![];
let (_, _) = pulldown_cmark::Parser::new_with_broken_link_callback(
@ -932,30 +925,28 @@ fn markdown_to_html(
(start_quote_count, end_quote_count)
}
Event::Start(CodeBlock(CodeBlockKind::Fenced(_))) => {
expecting_code_block = true;
docs_parser.push(event);
Event::Start(CodeBlock(_)) => {
in_code_block = true;
(0, 0)
}
Event::End(CodeBlock(_)) => {
expecting_code_block = false;
docs_parser.push(event);
if in_code_block {
let highlighted_html = roc_highlight::highlight_roc_code(&to_highlight);
docs_parser.push(pulldown_cmark::Event::Html(
pulldown_cmark::CowStr::from(highlighted_html),
));
to_highlight = String::new();
in_code_block = false;
}
(0, 0)
}
Event::Text(CowStr::Borrowed(code_str)) if expecting_code_block => {
match syntax_highlight_expr(
code_str
)
{
Ok(highlighted_code_str) => {
docs_parser.push(Event::Html(CowStr::from(highlighted_code_str)));
}
Err(syntax_error) => {
panic!("Unexpected parse failure when parsing this for rendering in docs:\n\n{}\n\nParse error was:\n\n{:?}\n\n", code_str, syntax_error)
}
};
Event::Text(t) => {
if in_code_block {
// If we're in a code block, build up the string of text
to_highlight.push_str(&t);
} else {
docs_parser.push(event);
}
(0, 0)
}
_ => {

View file

@ -21,8 +21,10 @@
--body-bg-color: #ffffff;
--border-color: #e9e9e9;
--faded-color: #4c4c4c;
--font-sans: -apple-system, BlinkMacSystemFont, Roboto, Helvetica, Arial, sans-serif;
--font-mono: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
--font-sans: -apple-system, BlinkMacSystemFont, Roboto, Helvetica, Arial,
sans-serif;
--font-mono: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier,
monospace;
--top-header-height: 67px;
--sidebar-width: 280px;
--top-bar-bg: #8257e5;
@ -37,7 +39,8 @@ a {
color: #972395;
}
table tr th, table tr td {
table tr th,
table tr td {
padding: 6px 13px;
}
@ -104,7 +107,8 @@ a {
text-decoration: none;
}
a:hover, a:hover code {
a:hover,
a:hover code {
text-decoration: underline;
}
@ -116,7 +120,8 @@ a:hover, a:hover code {
background-color: var(--top-bar-bg);
}
.pkg-and-logo a, .pkg-and-logo a:visited {
.pkg-and-logo a,
.pkg-and-logo a:visited {
color: var(--top-bar-fg);
}
@ -140,7 +145,9 @@ a:hover, a:hover code {
body {
display: grid;
grid-template-columns: [before-sidebar] 1fr [sidebar] var(--sidebar-width) [main-content] fit-content(calc(1280px - var(--sidebar-width))) [end] 1fr;
grid-template-columns: [before-sidebar] 1fr [sidebar] var(--sidebar-width) [main-content] fit-content(
calc(1280px - var(--sidebar-width))
) [end] 1fr;
grid-template-rows: [top-header] var(--top-header-height) [above-footer] auto [footer] auto;
box-sizing: border-box;
margin: 0;
@ -226,8 +233,8 @@ section > *:last-child {
}
p {
overflow-wrap: break-word;
margin: 24px 0;
overflow-wrap: break-word;
margin: 24px 0;
}
footer {
@ -290,7 +297,8 @@ footer p {
margin-bottom: 48px;
}
.module-name a, .module-name a:visited {
.module-name a,
.module-name a:visited {
color: inherit;
}
@ -311,7 +319,8 @@ footer p {
font-weight: bold;
}
a, a:visited {
a,
a:visited {
color: var(--link-color);
}
@ -350,14 +359,16 @@ p code {
border-radius: 4px;
}
code a, a code {
code a,
a code {
text-decoration: none;
color: var(--code-link-color);
background: none;
padding: 0;
}
code a:visited, a:visited code {
code a:visited,
a:visited code {
color: var(--code-link-color);
}
@ -376,23 +387,22 @@ pre {
}
.syntax-number {
color: #60B7BF;
color: #60b7bf;
}
.syntax-string {
color:#F7577C;
color: #f7577c;
}
.syntax-bracket {
color:#FF335F;
color: #ff335f;
}
.syntax-closure-dash,
.syntax-closure-arrow,
.syntax-operator
{
color: #ffffff;
.syntax-operator {
color: #ffffff;
}
.syntax-comma {
color: #9573E6;
color: #9573e6;
}
.syntax-comment {
color: #ff0000;
@ -404,7 +414,8 @@ pre {
height: 0;
}
#module-search, #module-search:focus {
#module-search,
#module-search:focus {
opacity: 1;
padding: 12px 16px;
height: 48px;
@ -495,6 +506,47 @@ pre {
html {
scrollbar-color: #444444 #2f2f2f;
}
samp .kw,
samp .pipe,
samp .backslash,
samp .arrow,
samp .backpass,
samp .brace,
samp .bracket,
samp .paren {
/* language keywords, e.g. `if` */
color: #00c3ff;
}
samp .op,
samp .comma,
samp .qmark,
samp .bar,
samp .colon {
/* operators, e.g. `+` */
color: #ff3966;
}
samp .str {
/* string literals */
color: var(--link-color);
}
code .str {
/* string literals */
color: var(--link-color);
}
/* autovar = automatic variable names in the repl, e.g. # val1 */
samp .comment,
samp .autovar {
color: #4ed86c;
}
samp .number {
color: #00c3ff;
}
}
@media only screen and (max-device-width: 480px) and (orientation: portrait) {
@ -582,7 +634,7 @@ pre {
body {
grid-template-columns: auto;
grid-template-rows: [top-header] var(--top-header-height) [before-sidebar] auto [sidebar] auto [above-footer] auto [footer] auto;
/* [before-sidebar] 1fr [sidebar] var(--sidebar-width) [main-content] fit-content(calc(1280px - var(--sidebar-width))) [end] 1fr; */
/* [before-sidebar] 1fr [sidebar] var(--sidebar-width) [main-content] fit-content(calc(1280px - var(--sidebar-width))) [end] 1fr; */
margin: 0;
min-width: 320px;
@ -606,3 +658,58 @@ pre {
padding-bottom: 12px;
}
}
samp .ann {
/* type annotation - purple in the repl */
color: #f384fd;
}
samp .autovar {
/* automatic variable names in the repl, e.g. # val1 */
color: #338545;
}
samp .kw,
samp .pipe,
samp .backslash,
samp .arrow,
samp .backpass,
samp .brace,
samp .bracket,
samp .paren {
/* language keywords, e.g. `if`*/
color: #004cc2;
}
samp .op,
samp .comma,
samp .qmark,
samp .bar,
samp .colon {
/* operators, e.g. `+` */
color: #c20000;
}
samp .number {
/* number literals */
color: #158086;
}
samp .str {
/* string literals */
color: #158086;
}
samp .str-esc,
samp .str-interp {
/* escapes inside string literals, e.g. \t */
color: #3474db;
}
samp .dim {
opacity: 0.55;
}
samp .comment {
color: #338545;
}