mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-02 07:20:55 +00:00
Improved rendering time (#190)
* Improved rendering time * Fix useless codes
This commit is contained in:
parent
bef88f1696
commit
ed47f17f3c
@ -1,4 +1,5 @@
|
|||||||
use std::fmt::{self, Display, Formatter};
|
use std::fmt::{self, Display, Formatter};
|
||||||
|
use std::io::{self, prelude::*};
|
||||||
use std::str;
|
use std::str;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
@ -37,12 +38,33 @@ where
|
|||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
MarkupDisplay::Unsafe(ref t) => escape(&t.to_string()).fmt(f),
|
MarkupDisplay::Unsafe(ref t) => {
|
||||||
|
let mut w = EscapeWriter { fmt: f };
|
||||||
|
write!(w, "{}", t).map_err(|_e| fmt::Error)
|
||||||
|
}
|
||||||
MarkupDisplay::Safe(ref t) => t.fmt(f),
|
MarkupDisplay::Safe(ref t) => t.fmt(f),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct EscapeWriter<'a, 'b: 'a> {
|
||||||
|
fmt: &'a mut fmt::Formatter<'b>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl io::Write for EscapeWriter<'_, '_> {
|
||||||
|
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
|
||||||
|
let escaped = Escaped { bytes };
|
||||||
|
escaped
|
||||||
|
.fmt(self.fmt)
|
||||||
|
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?;
|
||||||
|
Ok(bytes.len())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&mut self) -> std::io::Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn escape(s: &str) -> Escaped<'_> {
|
pub fn escape(s: &str) -> Escaped<'_> {
|
||||||
Escaped {
|
Escaped {
|
||||||
bytes: s.as_bytes(),
|
bytes: s.as_bytes(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user