-
Notifications
You must be signed in to change notification settings - Fork 750
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proposal for escaping within lexed content
- Loading branch information
http://jneen.net/
committed
Jun 3, 2019
1 parent
5030571
commit 931a5f9
Showing
8 changed files
with
130 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
If Formatter.enable_escape! is called, this allows escaping into html | ||
or the parent format with a special delimiter. For example: | ||
<!<span style="text-decoration: underline">!>underlined text!<!</span>!> |
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,53 @@ | ||
module Rouge | ||
module Lexers | ||
class Escape < Lexer | ||
tag 'escape' | ||
aliases 'esc' | ||
|
||
desc 'A generic lexer for including escaped content - see Formatter.enable_escape!' | ||
|
||
option :start, 'the beginning of the escaped section, default "<!"' | ||
option :end, 'the end of the escaped section, e.g. "!>"' | ||
option :lang, 'the language to lex in unescaped sections' | ||
|
||
def initialize(*) | ||
super | ||
@start = string_option(:start) { '<!' } | ||
@end = string_option(:end) { '!>' } | ||
@lang = lexer_option(:lang) { PlainText.new } | ||
end | ||
|
||
def to_start_regex | ||
@to_start_regex ||= /(.*?)(#{Regexp.escape(@start)})/m | ||
end | ||
|
||
def to_end_regex | ||
@to_end_regex ||= /(.*?)(#{Regexp.escape(@end)})/m | ||
end | ||
|
||
def stream_tokens(str, &b) | ||
stream = StringScanner.new(str) | ||
p :to_start_regex => to_start_regex | ||
p :to_end_regex => to_end_regex | ||
|
||
loop do | ||
if stream.scan(to_start_regex) | ||
puts "pre-escape: #{stream[1].inspect}" if @debug | ||
@lang.continue_lex(stream[1], &b) | ||
else | ||
# no more start delimiters, scan til the end | ||
@lang.continue_lex(stream.rest, &b) | ||
return | ||
end | ||
|
||
if stream.scan(to_end_regex) | ||
yield Token::Tokens::Escape, stream[1] | ||
else | ||
yield Token::Tokens::Escape, stream.rest | ||
return | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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,8 @@ | ||
If Formatter.enable_escape! is called, this allows escaping into html | ||
or the parent format with a special delimiter. For example, here is | ||
some <!<span style="text-decoration: underline">!>underlined text<!</span>!> that flows with the rest of the document. | ||
|
||
When visually speccing this, you should see the escaped portions rendered as an | ||
error. This is because for safety reasons we don't allow escaping to html without | ||
explicit opting in. If you provide the ?escape=1 option in the url, you should | ||
see underlined text. |