diff --git a/lib/rouge/demos/python b/lib/rouge/demos/python index 77a5cb3111..2f783f0c53 100644 --- a/lib/rouge/demos/python +++ b/lib/rouge/demos/python @@ -1,6 +1,19 @@ +lazy import re + + def fib(n): # write Fibonacci series up to n """Print a Fibonacci series up to n.""" a, b = 0, 1 while a < n: print a, a, b = b, a+b + + +_ANSI_RE = None + +def strip_ansi(s: str) -> str: + """Remove ANSI escapes from a string.""" + if _ANSI_RE is None: + global _ANSI_RE + _ANSI_RE = re.compile(r"\033\[[;?0-9]*[a-zA-Z]") + return _ANSI_RE.sub("", s) diff --git a/lib/rouge/lexers/python.rb b/lib/rouge/lexers/python.rb index 39619363f2..884bb23e4f 100644 --- a/lib/rouge/lexers/python.rb +++ b/lib/rouge/lexers/python.rb @@ -115,6 +115,11 @@ def current_string rule %r/class\b/, Keyword, :classname + # handle `lazy import` soft keyword usage + # since `import` is a hard keyword, we know that this usage has to be + # the soft-keyword case + rule %r/lazy\b(?=#{inline_ws}import\b)/, Keyword + rule %r/`.*?`/, Str::Backtick rule %r/([rtfbu]{0,2})('''|"""|['"])/i do |m| groups Str::Affix, Str::Heredoc @@ -173,6 +178,7 @@ def current_string # import after from, meaning we don't push the :import state state :from_import do mixin :inline_whitespace + rule %r/lazy\b/, Keyword::Namespace rule %r/import\b/, Keyword::Namespace, :pop! rule(//) { pop! } end diff --git a/spec/visual/samples/python b/spec/visual/samples/python index af07996312..f28436dda0 100644 --- a/spec/visual/samples/python +++ b/spec/visual/samples/python @@ -262,3 +262,13 @@ match (100, 200): thing \ : other + + +lazy = "deferred" +lazy import requests +lazy \ + import \ + re +from typing lazy import Iterator +from typing lazy \ + import Generator