-
-
Notifications
You must be signed in to change notification settings - Fork 64
New feature: indenting the text in "context.write()" relative to the indentation of the entry of a python code block ("<%") #431
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ def __init__(self, buffer, **data): | |
self._with_template = None | ||
self._outputting_as_unicode = None | ||
self.namespaces = {} | ||
self._code_block_entry_mark = None | ||
|
||
# "capture" function which proxies to the | ||
# generic "capture" function | ||
|
@@ -137,10 +138,28 @@ def get(self, key, default=None): | |
|
||
return self._data.get(key, builtins.__dict__.get(key, default)) | ||
|
||
def write(self, string): | ||
def code_block_entry_mark(self): | ||
"""Mark the current location in the buffer.""" | ||
self._code_block_entry_mark = self._buffer_stack[-1].getpos() | ||
|
||
def write(self, string, indent_relative_to_code_block_entry=False): | ||
"""Write a string to this :class:`.Context` object's | ||
underlying output buffer.""" | ||
|
||
if (len(string) == 0): | ||
return | ||
|
||
print(f"write({string}, {indent_relative_to_code_block_entry}, {self._code_block_entry_mark})") | ||
|
||
if ((indent_relative_to_code_block_entry) and (self._code_block_entry_mark is not None)): | ||
text_before_code_block_entry = self._buffer_stack[-1].getvalue(self._code_block_entry_mark) | ||
indent_len = len(text_before_code_block_entry)-(text_before_code_block_entry.rfind("\n") + 1) | ||
indent = " " * indent_len | ||
|
||
text_before_write = self._buffer_stack[-1].getvalue() | ||
current_indent_len = len(text_before_write)-(text_before_write.rfind("\n") + 1) | ||
|
||
string = (" " * max(len(indent) - current_indent_len, 0)) + string[:-1].replace("\n", "\n" + indent) + string[-1] | ||
|
||
self._buffer_stack[-1].write(string) | ||
|
||
def writer(self): | ||
|
@@ -156,6 +175,7 @@ def _copy(self): | |
c._with_template = self._with_template | ||
c._outputting_as_unicode = self._outputting_as_unicode | ||
c.namespaces = self.namespaces | ||
c._code_block_entry_mark = self._code_block_entry_mark | ||
c.caller_stack = self.caller_stack | ||
return c | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
# the MIT License: http://www.opensource.org/licenses/mit-license.php | ||
from ast import parse | ||
import codecs | ||
import collections | ||
import operator | ||
import os | ||
import re | ||
|
@@ -143,23 +142,26 @@ class FastEncodingBuffer: | |
and supports unicode data.""" | ||
|
||
def __init__(self, encoding=None, errors="strict"): | ||
self.data = collections.deque() | ||
self.data = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this has to remain a deque(). a deque is much more efficient for what we're doing here |
||
self.encoding = encoding | ||
self.delim = "" | ||
self.errors = errors | ||
self.write = self.data.append | ||
|
||
def truncate(self): | ||
self.data = collections.deque() | ||
self.data = [] | ||
self.write = self.data.append | ||
|
||
def getvalue(self): | ||
def getvalue(self, uptopos=None): | ||
if self.encoding: | ||
return self.delim.join(self.data).encode( | ||
return self.delim.join(self.data[:uptopos]).encode( | ||
self.encoding, self.errors | ||
) | ||
else: | ||
return self.delim.join(self.data) | ||
return self.delim.join(self.data[:uptopos]) | ||
|
||
def getpos(self): | ||
return len(self.data) | ||
|
||
|
||
class LRUCache(dict): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this new argument needs to be keyword only:
I think the name
indent_relative_to_code_block_entry
is too long of a name. needs to be a short name.