diff --git a/Cargo.lock b/Cargo.lock index 2ba9298b..8c9fb342 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1298,9 +1298,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.18" +version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "518ef76f2f87365916b142844c16d8fefd85039bc5699050210a7778ee1cd1de" +checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" [[package]] name = "markdown" @@ -1811,9 +1811,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.37.19" +version = "0.37.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acf8729d8542766f1b2cf77eb034d52f40d375bb8b615d0b147089946e16613d" +checksum = "b96e891d04aa506a6d1f318d2771bcb1c7dfda84e126660ace067c9b474bb2c0" dependencies = [ "bitflags", "errno", @@ -2010,7 +2010,7 @@ dependencies = [ [[package]] name = "sqlpage" -version = "0.6.11" +version = "0.6.12" dependencies = [ "actix-web", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 51cf8d47..14b9009d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sqlpage" -version = "0.6.11" +version = "0.6.12" edition = "2021" description = "A SQL-only web application framework. Takes .sql files and formats the query result using pre-made configurable professional-looking components." keywords = ["web", "sql", "framework"] diff --git a/examples/official-site/sqlpage/migrations/01_documentation.sql b/examples/official-site/sqlpage/migrations/01_documentation.sql index 000a0ced..eade5fd6 100644 --- a/examples/official-site/sqlpage/migrations/01_documentation.sql +++ b/examples/official-site/sqlpage/migrations/01_documentation.sql @@ -317,14 +317,22 @@ INSERT INTO component(name, icon, description) VALUES INSERT INTO parameter(component, name, description, type, top_level, optional) SELECT 'table', * FROM (VALUES -- top level ('sort', 'Make the columns clickable to let the user sort by the value contained in the column.', 'BOOLEAN', TRUE, TRUE), - ('search', 'Add a search bar at the top of the table, letting users easily filter table rows by value.', 'BOOLEAN', TRUE, TRUE) + ('search', 'Add a search bar at the top of the table, letting users easily filter table rows by value.', 'BOOLEAN', TRUE, TRUE), + ('markdown', 'Set this to the name of a column whose content should be interpreted as markdown . Used to display rich text with links in the table. This argument can be repeated multiple times to intepret multiple columns as markdown.', 'TEXT', TRUE, TRUE) ) x; INSERT INTO example(component, description, properties) VALUES + ('table', 'The most basic table.', + json('[{"component":"table"}, {"a": 1, "b": 2}, {"a": 3, "b": 4}]')), ('table', 'A table of users with filtering and sorting.', json('[{"component":"table", "sort":true, "search":true}, '|| '{"Forename": "Ophir", "Surname": "Lojkine", "Pseudonym": "lovasoa"},' || - '{"Forename": "Linus", "Surname": "Torvalds", "Pseudonym": "torvalds"}]')); + '{"Forename": "Linus", "Surname": "Torvalds", "Pseudonym": "torvalds"}]')), + ('table', 'A table that uses markdown to display links', + json('[{"component":"table", "markdown": "Documentation"}, '|| + '{"name": "table", "description": "Displays SQL results as a searchable table.", "Documentation": "[docs](documentation.sql?component=table)"}, + {"name": "chart", "description": "Show graphs based on numeric data.", "Documentation": "[docs](documentation.sql?component=chart)"} + ]')); INSERT INTO component(name, icon, description) VALUES diff --git a/sqlpage/templates/table.handlebars b/sqlpage/templates/table.handlebars index 9d8220d9..b1fd994b 100644 --- a/sqlpage/templates/table.handlebars +++ b/sqlpage/templates/table.handlebars @@ -27,7 +27,13 @@ {{~#each this~}} - {{this}} + + {{~#if (array_contains ../../markdown @key)~}} + {{{markdown this}}} + {{~else~}} + {{this}} + {{~/if~}} + {{~/each~}} {{~/each_row~}} diff --git a/src/templates.rs b/src/templates.rs index 0160e079..1543aa41 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -190,6 +190,8 @@ impl AllTemplates { }); handlebars.register_helper("entries", Box::new(entries)); + + // delay helper: store a piece of information in memory that can be output later with flush_delayed handlebars.register_helper("delay", Box::new(delay_helper)); handlebars.register_helper("flush_delayed", Box::new(flush_delayed_helper)); @@ -201,16 +203,28 @@ impl AllTemplates { handlebars.register_helper("sum", Box::new(sum_helper)); + // to_array: convert a value to a single-element array. If the value is already an array, return it as-is. handlebars_helper!(to_array: |x: Json| match x { JsonValue::Array(arr) => arr.clone(), other => vec![other.clone()] }); handlebars.register_helper("to_array", Box::new(to_array)); + // array_contains: check if an array contains an element. If the first argument is not an array, it is compared to the second argument. + handlebars_helper!(array_contains: |array: Json, element: Json| match array { + JsonValue::Array(arr) => arr.contains(element), + other => other == element + }); + handlebars.register_helper("array_contains", Box::new(array_contains)); + + // static_path helper: generate a path to a static file. Replaces sqpage.js by sqlpage..js handlebars_helper!(static_path: |x: str| match x { "sqlpage.js" => static_filename!("sqlpage.js"), "sqlpage.css" => static_filename!("sqlpage.css"), - _ => "!!unknown static path!!" + unknown => { + log::error!("Unknown static path: {}", unknown); + "!!unknown static path!!" + } }); handlebars.register_helper("static_path", Box::new(static_path));