Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move all of the template creation into askama_shared #647

Merged
merged 6 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions askama/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "askama"
version = "0.11.1"
version = "0.11.2"
description = "Type-safe, compiled Jinja-like templates for Rust"
documentation = "https://docs.rs/askama"
keywords = ["markup", "template", "jinja2", "html"]
Expand All @@ -17,29 +17,29 @@ maintenance = { status = "actively-developed" }

[features]
default = ["config", "humansize", "num-traits", "urlencode"]
config = ["askama_derive/config", "askama_shared/config"]
config = ["askama_shared/config"]
humansize = ["askama_shared/humansize"]
markdown = ["askama_shared/markdown"]
urlencode = ["askama_shared/percent-encoding"]
serde-json = ["askama_derive/json", "askama_shared/json"]
serde-yaml = ["askama_derive/yaml", "askama_shared/yaml"]
serde-json = ["askama_shared/json"]
serde-yaml = ["askama_shared/yaml"]
num-traits = ["askama_shared/num-traits"]
with-actix-web = ["askama_derive/actix-web"]
with-axum = ["askama_derive/axum"]
with-gotham = ["askama_derive/gotham"]
with-mendes = ["askama_derive/mendes"]
with-rocket = ["askama_derive/rocket"]
with-tide = ["askama_derive/tide"]
with-warp = ["askama_derive/warp"]
with-actix-web = ["askama_shared/actix-web"]
with-axum = ["askama_shared/axum"]
with-gotham = ["askama_shared/gotham"]
with-mendes = ["askama_shared/mendes"]
with-rocket = ["askama_shared/rocket"]
with-tide = ["askama_shared/tide"]
with-warp = ["askama_shared/warp"]

# deprecated
mime = []
mime_guess = []

[dependencies]
askama_derive = { version = "0.11.2", path = "../askama_derive" }
askama_escape = { version = "0.10", path = "../askama_escape" }
askama_shared = { version = "0.12.1", path = "../askama_shared", default-features = false }
askama_derive = { version = "0.12.0", path = "../askama_derive" }
askama_escape = { version = "0.10.3", path = "../askama_escape" }
askama_shared = { version = "0.13.0", path = "../askama_shared", default-features = false }

[package.metadata.docs.rs]
features = ["config", "humansize", "num-traits", "serde-json", "serde-yaml"]
4 changes: 1 addition & 3 deletions askama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ impl<T: Template> DynTemplate for T {
}
}

pub use crate::shared::filters;
pub use crate::shared::helpers;
pub use crate::shared::{read_config_file, Error, MarkupDisplay, Result};
pub use crate::shared::{filters, helpers, Error, MarkupDisplay, Result};
pub use askama_derive::*;

#[deprecated(since = "0.11.1", note = "The only function in this mod is deprecated")]
Expand Down
5 changes: 2 additions & 3 deletions askama_actix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "askama_actix"
version = "0.13.0"
version = "0.13.1"
description = "Actix-Web integration for Askama templates"
documentation = "https://docs.rs/askama"
keywords = ["markup", "template", "jinja2", "html"]
Expand All @@ -14,8 +14,7 @@ edition = "2018"

[dependencies]
actix-web = { version = "4", default-features = false }
askama = { version = "0.11.1", path = "../askama", default-features = false, features = ["with-actix-web"] }
askama_shared = { version = "0.12.2", path = "../askama_shared" }
askama = { version = "0.11.2", path = "../askama", default-features = false, features = ["with-actix-web"] }

[dev-dependencies]
actix-rt = { version = "2", default-features = false }
Expand Down
19 changes: 2 additions & 17 deletions askama_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "askama_derive"
version = "0.11.2"
version = "0.12.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this bump?

Copy link
Collaborator Author

@Kijewski Kijewski Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I un-exported a bunch of symbols. Older askama_shared versions would break with this update. Every item in https://github.com/djc/askama/blob/main/askama_derive/src/lib.rs#L8-L10 is gone after the PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the features section, because they are now handles in askama_shared.

I am not 100% sure, but I guess if I re-added the features, incrementing the patch version only should be fine.

description = "Procedural macro package for Askama"
homepage = "https://github.com/djc/askama"
repository = "https://github.com/djc/askama"
Expand All @@ -12,20 +12,5 @@ edition = "2018"
[lib]
proc-macro = true

[features]
config = ["askama_shared/config"]
json = ["askama_shared/json"]
yaml = ["askama_shared/yaml"]

actix-web = []
axum = []
gotham = []
mendes = []
rocket = []
tide = []
warp = []

[dependencies]
askama_shared = { version = "0.12.1", path = "../askama_shared", default-features = false }
proc-macro2 = "1"
syn = "1"
askama_shared = { version = "0.13.0", path = "../askama_shared", default-features = false }
112 changes: 1 addition & 111 deletions askama_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,119 +2,9 @@
#![deny(elided_lifetimes_in_paths)]
#![deny(unreachable_pub)]

use askama_shared::heritage::{Context, Heritage};
use askama_shared::input::{Print, Source, TemplateInput};
use askama_shared::parser::{parse, Expr, Node};
use askama_shared::{
generator, get_template_source, read_config_file, CompileError, Config, Integrations,
};
use proc_macro::TokenStream;

use std::collections::HashMap;
use std::path::PathBuf;

#[proc_macro_derive(Template, attributes(template))]
pub fn derive_template(input: TokenStream) -> TokenStream {
let ast: syn::DeriveInput = syn::parse(input).unwrap();
match build_template(&ast) {
Ok(source) => source.parse().unwrap(),
Err(e) => e.to_compile_error().into(),
}
}

/// Takes a `syn::DeriveInput` and generates source code for it
///
/// Reads the metadata from the `template()` attribute to get the template
/// metadata, then fetches the source from the filesystem. The source is
/// parsed, and the parse tree is fed to the code generator. Will print
/// the parse tree and/or generated source according to the `print` key's
/// value as passed to the `template()` attribute.
fn build_template(ast: &syn::DeriveInput) -> Result<String, CompileError> {
let config_toml = read_config_file()?;
let config = Config::new(&config_toml)?;
let input = TemplateInput::new(ast, &config)?;
let source: String = match input.source {
Source::Source(ref s) => s.clone(),
Source::Path(_) => get_template_source(&input.path)?,
};

let mut sources = HashMap::new();
find_used_templates(&input, &mut sources, source)?;

let mut parsed = HashMap::new();
for (path, src) in &sources {
parsed.insert(path.as_path(), parse(src, input.syntax)?);
}

let mut contexts = HashMap::new();
for (path, nodes) in &parsed {
contexts.insert(*path, Context::new(input.config, path, nodes)?);
}

let ctx = &contexts[input.path.as_path()];
let heritage = if !ctx.blocks.is_empty() || ctx.extends.is_some() {
Some(Heritage::new(ctx, &contexts))
} else {
None
};

if input.print == Print::Ast || input.print == Print::All {
eprintln!("{:?}", parsed[input.path.as_path()]);
}

let code = generator::generate(&input, &contexts, heritage.as_ref(), INTEGRATIONS)?;
if input.print == Print::Code || input.print == Print::All {
eprintln!("{}", code);
}
Ok(code)
askama_shared::derive_template(input.into()).into()
}

fn find_used_templates(
input: &TemplateInput<'_>,
map: &mut HashMap<PathBuf, String>,
source: String,
) -> Result<(), CompileError> {
let mut dependency_graph = Vec::new();
let mut check = vec![(input.path.clone(), source)];
while let Some((path, source)) = check.pop() {
for n in parse(&source, input.syntax)? {
match n {
Node::Extends(Expr::StrLit(extends)) => {
let extends = input.config.find_template(extends, Some(&path))?;
let dependency_path = (path.clone(), extends.clone());
if dependency_graph.contains(&dependency_path) {
return Err(format!(
"cyclic dependecy in graph {:#?}",
dependency_graph
.iter()
.map(|e| format!("{:#?} --> {:#?}", e.0, e.1))
.collect::<Vec<String>>()
)
.into());
}
dependency_graph.push(dependency_path);
let source = get_template_source(&extends)?;
check.push((extends, source));
}
Node::Import(_, import, _) => {
let import = input.config.find_template(import, Some(&path))?;
let source = get_template_source(&import)?;
check.push((import, source));
}
_ => {}
}
}
map.insert(path, source);
}
Ok(())
}

const INTEGRATIONS: Integrations = Integrations {
actix: cfg!(feature = "actix-web"),
axum: cfg!(feature = "axum"),
gotham: cfg!(feature = "gotham"),
mendes: cfg!(feature = "mendes"),
rocket: cfg!(feature = "rocket"),
tide: cfg!(feature = "tide"),
warp: cfg!(feature = "warp"),
};
10 changes: 9 additions & 1 deletion askama_shared/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "askama_shared"
version = "0.12.2"
version = "0.13.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this bump?

Copy link
Collaborator Author

@Kijewski Kijewski Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I un-exported a bunch of symbols. Older askama_derive versions would break with this update. Every item in https://github.com/djc/askama/blob/main/askama_derive/src/lib.rs#L8-L10 is gone after the PR.

description = "Shared code for Askama"
homepage = "https://github.com/djc/askama"
repository = "https://github.com/djc/askama"
Expand All @@ -16,6 +16,14 @@ json = ["serde", "serde_json", "askama_escape/json"]
markdown = ["comrak"]
yaml = ["serde", "serde_yaml"]

actix-web = []
axum = []
gotham = []
mendes = []
rocket = []
tide = []
warp = []

[dependencies]
askama_escape = { version = "0.10.3", path = "../askama_escape" }
comrak = { version = "0.12", optional = true, default-features = false }
Expand Down
Loading