From e2045660626e57948cfb60137b0f5bb0d19b00a2 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 23 May 2018 21:28:12 +0200 Subject: [PATCH] Added basic example with includes --- content/docs/application.md | 16 +--------------- examples/.gitignore | 1 + examples/Cargo.toml | 4 ++++ examples/application/Cargo.toml | 6 ++++++ examples/application/src/main.rs | 15 +++++++++++++++ layouts/shortcodes/include-example.html | 7 +++++++ 6 files changed, 34 insertions(+), 15 deletions(-) create mode 100644 examples/.gitignore create mode 100644 examples/Cargo.toml create mode 100644 examples/application/Cargo.toml create mode 100644 examples/application/src/main.rs create mode 100644 layouts/shortcodes/include-example.html diff --git a/content/docs/application.md b/content/docs/application.md index 401df9f..8da3203 100644 --- a/content/docs/application.md +++ b/content/docs/application.md @@ -23,21 +23,7 @@ The prefix should consist of value path segments. > any request with the paths `/app`, `/app/`, or `/app/test` would match; > however, the path `/application` would not match. -```rust -# extern crate actix_web; -# use actix_web::{App, Responder, HttpRequest, http::Method}; - -fn index(req: HttpRequest) -> impl Responder { - "Hello world!" -} - -fn main() { - let app = App::new() - .prefix("/app") - .resource("/index.html", |r| r.method(Method::GET).f(index)) - .finish(); -} -``` +{{< include-example example="application" section="setup" >}} In this example, an application with the `/app` prefix and a `index.html` resource are created. This resource is available through the `/app/index.html` url. diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1 @@ +target diff --git a/examples/Cargo.toml b/examples/Cargo.toml new file mode 100644 index 0000000..2cb1e30 --- /dev/null +++ b/examples/Cargo.toml @@ -0,0 +1,4 @@ +[workspace] +members = [ + "application", +] diff --git a/examples/application/Cargo.toml b/examples/application/Cargo.toml new file mode 100644 index 0000000..0349037 --- /dev/null +++ b/examples/application/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "application" +version = "0.1.0" + +[dependencies] +actix-web = "0.6" diff --git a/examples/application/src/main.rs b/examples/application/src/main.rs new file mode 100644 index 0000000..65442a9 --- /dev/null +++ b/examples/application/src/main.rs @@ -0,0 +1,15 @@ +extern crate actix_web; +use actix_web::{App, Responder, HttpRequest, http::Method}; + +// +fn index(req: HttpRequest) -> impl Responder { + "Hello world!" +} + +fn main() { + let app = App::new() + .prefix("/app") + .resource("/index.html", |r| r.method(Method::GET).f(index)) + .finish(); +} +// diff --git a/layouts/shortcodes/include-example.html b/layouts/shortcodes/include-example.html new file mode 100644 index 0000000..ca8b29d --- /dev/null +++ b/layouts/shortcodes/include-example.html @@ -0,0 +1,7 @@ +{{$example := .Get "example"}} +{{$file := .Get "file" | default "main.rs"}} +{{$section := .Get "section"}} +{{$source := readFile (printf "examples/%s/src/%s" $example $file)}} +{{$source := index (findRE (printf "(?ms)// <%s>$(.*?)// " $section $section) $source) 0 }} +{{$source := trim (replaceRE "// <[^>]+>" "" $source) "\n" }} +{{- highlight $source "rust" "" -}}