This repository has been archived by the owner on Feb 12, 2018. It is now read-only.
forked from trishume/syntect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
216 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! HTML Escaping | ||
//! | ||
//! This module contains one unit-struct which can be used to HTML-escape a | ||
//! string of text (for use in a format string). | ||
use std::fmt; | ||
|
||
/// Wrapper struct which will emit the HTML-escaped version of the contained | ||
/// string when passed to a format string. | ||
pub struct Escape<'a>(pub &'a str); | ||
|
||
impl<'a> fmt::Display for Escape<'a> { | ||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
// Because the internet is always right, turns out there's not that many | ||
// characters to escape: http://stackoverflow.com/questions/7381974 | ||
let Escape(s) = *self; | ||
let pile_o_bits = s; | ||
let mut last = 0; | ||
for (i, ch) in s.bytes().enumerate() { | ||
match ch as char { | ||
'<' | '>' | '&' | '\'' | '"' => { | ||
try!(fmt.write_str(&pile_o_bits[last.. i])); | ||
let s = match ch as char { | ||
'>' => ">", | ||
'<' => "<", | ||
'&' => "&", | ||
'\'' => "'", | ||
'"' => """, | ||
_ => unreachable!() | ||
}; | ||
try!(fmt.write_str(s)); | ||
last = i + 1; | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
if last < s.len() { | ||
try!(fmt.write_str(&pile_o_bits[last..])); | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<pre style="background-color:#2b303b;"> | ||
<span style="color:#c0c5ce;"><</span><span style="color:#bf616a;">script</span><span style="color:#c0c5ce;"> </span><span style="color:#d08770;">type</span><span style="color:#c0c5ce;">=</span><span style="color:#c0c5ce;">"</span><span style="color:#a3be8c;">text/javascript</span><span style="color:#c0c5ce;">"</span><span style="color:#c0c5ce;">></span> | ||
<span style="color:#c0c5ce;"> </span><span style="color:#b48ead;">var</span><span style="color:#c0c5ce;"> </span><span style="color:#bf616a;">lol</span><span style="color:#c0c5ce;"> </span><span style="color:#c0c5ce;">=</span><span style="color:#c0c5ce;"> </span><span style="color:#c0c5ce;">"</span><span style="color:#a3be8c;">JS nesting</span><span style="color:#c0c5ce;">"</span><span style="color:#c0c5ce;">;</span> | ||
<span style="color:#c0c5ce;"> </span><span style="color:#b48ead;">class</span><span style="color:#eff1f5;"> </span><span style="color:#ebcb8b;">WithES6</span><span style="color:#eff1f5;"> </span><span style="color:#b48ead;">extends</span><span style="color:#eff1f5;"> </span><span style="color:#a3be8c;">THREE</span><span style="color:#eff1f5;">.</span><span style="color:#a3be8c;">Mesh</span><span style="color:#eff1f5;"> </span><span style="color:#eff1f5;">{</span> | ||
<span style="color:#eff1f5;"> </span><span style="color:#b48ead;">static</span><span style="color:#eff1f5;"> </span><span style="color:#8fa1b3;">highQuality</span><span style="color:#c0c5ce;">(</span><span style="color:#c0c5ce;">)</span><span style="color:#eff1f5;"> </span><span style="color:#eff1f5;">{</span><span style="color:#eff1f5;"> </span><span style="color:#65737e;">//</span><span style="color:#65737e;"> such classes</span> | ||
<span style="color:#eff1f5;"> </span><span style="color:#b48ead;">return</span><span style="color:#eff1f5;"> </span><span style="color:#bf616a;">this</span><span style="color:#eff1f5;">.</span><span style="color:#bf616a;">toString</span><span style="color:#eff1f5;">(</span><span style="color:#eff1f5;">)</span><span style="color:#eff1f5;">;</span> | ||
<span style="color:#eff1f5;"> </span><span style="color:#eff1f5;">}</span> | ||
<span style="color:#eff1f5;"> </span><span style="color:#eff1f5;">}</span> | ||
<span style="color:#c0c5ce;"> </span><span style="color:#ab7967;"><%</span> | ||
<span style="color:#65737e;"> </span><span style="color:#65737e;">#</span><span style="color:#65737e;"> The outer syntax is HTML (Rails) detected from the .erb extension</span> | ||
<span style="color:#c0c5ce;"> puts </span><span style="color:#c0c5ce;">"</span><span style="color:#a3be8c;">Ruby </span><span style="color:#ab7967;">#{</span><span style="color:#c0c5ce;">'</span><span style="color:#a3be8c;">nesting</span><span style="color:#c0c5ce;">'</span><span style="color:#a3be8c;"> </span><span style="color:#c0c5ce;">*</span><span style="color:#a3be8c;"> </span><span style="color:#d08770;">2</span><span style="color:#ab7967;">}</span><span style="color:#c0c5ce;">"</span> | ||
<span style="color:#c0c5ce;"> here </span><span style="color:#c0c5ce;">=</span><span style="color:#c0c5ce;"> </span><span style="color:#c0c5ce;"><<-WOWCOOL</span> | ||
<span style="color:#a3be8c;"> high quality parsing even supports custom heredoc endings</span> | ||
<span style="color:#a3be8c;"> </span><span style="color:#ab7967;">#{</span> | ||
<span style="color:#a3be8c;"> nested </span><span style="color:#c0c5ce;">=</span><span style="color:#a3be8c;"> </span><span style="color:#d08770;">5</span><span style="color:#a3be8c;"> </span><span style="color:#c0c5ce;">*</span><span style="color:#a3be8c;"> </span><span style="color:#c0c5ce;"><<-ZOMG</span> | ||
<span style="color:#a3be8c;"> nested heredocs! (no highlighting: 5 * 6, yes highlighting: </span><span style="color:#ab7967;">#{</span><span style="color:#d08770;">5</span><span style="color:#a3be8c;"> </span><span style="color:#c0c5ce;">*</span><span style="color:#a3be8c;"> </span><span style="color:#d08770;">6</span><span style="color:#ab7967;">}</span><span style="color:#a3be8c;">)</span> | ||
<span style="color:#c0c5ce;"> ZOMG</span> | ||
<span style="color:#a3be8c;"> </span><span style="color:#ab7967;">}</span> | ||
<span style="color:#c0c5ce;"> WOWCOOL</span> | ||
<span style="color:#c0c5ce;"> sql </span><span style="color:#c0c5ce;">=</span><span style="color:#c0c5ce;"> </span><span style="color:#c0c5ce;"><<-SQL</span> | ||
<span style="color:#a3be8c;"> </span><span style="color:#b48ead;">select</span><span style="color:#a3be8c;"> </span><span style="color:#c0c5ce;">*</span><span style="color:#a3be8c;"> </span><span style="color:#b48ead;">from</span><span style="color:#a3be8c;"> heredocs </span><span style="color:#b48ead;">where</span><span style="color:#a3be8c;"> there_are_special_heredoc_names </span><span style="color:#c0c5ce;">=</span><span style="color:#a3be8c;"> </span><span style="color:#d08770;">true</span> | ||
<span style="color:#c0c5ce;"> SQL</span> | ||
<span style="color:#c0c5ce;"> </span><span style="color:#ab7967;">%></span> | ||
<span style="color:#c0c5ce;"></</span><span style="color:#bf616a;">script</span><span style="color:#c0c5ce;">></span> | ||
<span style="color:#c0c5ce;"><</span><span style="color:#bf616a;">style</span><span style="color:#c0c5ce;"> </span><span style="color:#d08770;">type</span><span style="color:#c0c5ce;">=</span><span style="color:#c0c5ce;">"</span><span style="color:#a3be8c;">text/css</span><span style="color:#c0c5ce;">"</span><span style="color:#c0c5ce;">></span> | ||
<span style="color:#c0c5ce;"> </span><span style="color:#65737e;">/*</span><span style="color:#65737e;"> the HTML syntax also supports CSS of course </span><span style="color:#65737e;">*/</span> | ||
<span style="color:#b48ead;"> .stuff #wow </span><span style="color:#c0c5ce;">{</span> | ||
<span style="color:#c0c5ce;"> </span><span style="color:#c0c5ce;">border</span><span style="color:#c0c5ce;">:</span><span style="color:#c0c5ce;"> 5px </span><span style="color:#96b5b4;">#</span><span style="color:#96b5b4;">ffffff</span><span style="color:#c0c5ce;">;</span> | ||
<span style="color:#c0c5ce;"> </span><span style="color:#c0c5ce;">background</span><span style="color:#c0c5ce;">:</span><span style="color:#c0c5ce;"> </span><span style="color:#96b5b4;">url</span><span style="color:#c0c5ce;">(</span><span style="color:#c0c5ce;">"</span><span style="color:#a3be8c;">wow</span><span style="color:#c0c5ce;">"</span><span style="color:#c0c5ce;">)</span><span style="color:#c0c5ce;">;</span> | ||
<span style="color:#c0c5ce;"> </span><span style="color:#c0c5ce;">}</span> | ||
<span style="color:#c0c5ce;"></</span><span style="color:#bf616a;">style</span><span style="color:#c0c5ce;">></span> | ||
</pre> |