(
reader: _TemplateReader,
template: Template,
in_block: Optional[str] = None,
in_loop: Optional[str] = None,
)
| 845 | |
| 846 | |
| 847 | def _parse( |
| 848 | reader: _TemplateReader, |
| 849 | template: Template, |
| 850 | in_block: Optional[str] = None, |
| 851 | in_loop: Optional[str] = None, |
| 852 | ) -> _ChunkList: |
| 853 | body = _ChunkList([]) |
| 854 | while True: |
| 855 | # Find next template directive |
| 856 | curly = 0 |
| 857 | while True: |
| 858 | curly = reader.find("{", curly) |
| 859 | if curly == -1 or curly + 1 == reader.remaining(): |
| 860 | # EOF |
| 861 | if in_block: |
| 862 | reader.raise_parse_error( |
| 863 | "Missing {%% end %%} block for %s" % in_block |
| 864 | ) |
| 865 | body.chunks.append( |
| 866 | _Text(reader.consume(), reader.line, reader.whitespace) |
| 867 | ) |
| 868 | return body |
| 869 | # If the first curly brace is not the start of a special token, |
| 870 | # start searching from the character after it |
| 871 | if reader[curly + 1] not in ("{", "%", "#"): |
| 872 | curly += 1 |
| 873 | continue |
| 874 | # When there are more than 2 curlies in a row, use the |
| 875 | # innermost ones. This is useful when generating languages |
| 876 | # like latex where curlies are also meaningful |
| 877 | if ( |
| 878 | curly + 2 < reader.remaining() |
| 879 | and reader[curly + 1] == "{" |
| 880 | and reader[curly + 2] == "{" |
| 881 | ): |
| 882 | curly += 1 |
| 883 | continue |
| 884 | break |
| 885 | |
| 886 | # Append any text before the special token |
| 887 | if curly > 0: |
| 888 | cons = reader.consume(curly) |
| 889 | body.chunks.append(_Text(cons, reader.line, reader.whitespace)) |
| 890 | |
| 891 | start_brace = reader.consume(2) |
| 892 | line = reader.line |
| 893 | |
| 894 | # Template directives may be escaped as "{{!" or "{%!". |
| 895 | # In this case output the braces and consume the "!". |
| 896 | # This is especially useful in conjunction with jquery templates, |
| 897 | # which also use double braces. |
| 898 | if reader.remaining() and reader[0] == "!": |
| 899 | reader.consume(1) |
| 900 | body.chunks.append(_Text(start_brace, line, reader.whitespace)) |
| 901 | continue |
| 902 | |
| 903 | # Comment |
| 904 | if start_brace == "{#": |
no test coverage detected
searching dependent graphs…