Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 975380d

Browse files
Merge pull request trentm#427 from Crozzers/fix-issue-426
Fix fenced code blocks breaking lists (issue trentm#426)
2 parents c986c26 + a16084d commit 975380d

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

lib/markdown2.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,10 +1885,17 @@ def unhash_code(codeblock):
18851885
return codeblock
18861886
lexer = self._get_pygments_lexer(lexer_name)
18871887
if lexer:
1888+
# calculate code block's leading indent to not break lists
1889+
leading_indent = re.match(r'[ \t]*(?=`{3,})', match.group(1))
1890+
if leading_indent is not None:
1891+
leading_indent = leading_indent.group(0)
1892+
else:
1893+
leading_indent = ''
1894+
18881895
codeblock = unhash_code( codeblock )
18891896
colored = self._color_with_pygments(codeblock, lexer,
18901897
**formatter_opts)
1891-
return "\n\n%s\n\n" % colored
1898+
return "\n\n%s%s\n\n" % (leading_indent, colored)
18921899

18931900
codeblock = self._encode_code(codeblock)
18941901
pre_class_str = self._html_class_str_from_tag("pre")
@@ -1936,7 +1943,7 @@ def _do_code_blocks(self, text):
19361943

19371944
_fenced_code_block_re = re.compile(r'''
19381945
(?:\n+|\A\n?|(?<=\n))
1939-
(^`{3,})\s{0,99}?([\w+-]+)?\s{0,99}?\n # $1 = opening fence (captured for back-referencing), $2 = optional lang
1946+
(^[ \t]*`{3,})\s{0,99}?([\w+-]+)?\s{0,99}?\n # $1 = opening fence (captured for back-referencing), $2 = optional lang
19401947
(.*?) # $3 = code block content
19411948
\1[ \t]*\n # closing fence
19421949
''', re.M | re.X | re.S)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<h1>Django Templates</h1>
2+
3+
<h2>NOTES</h2>
4+
5+
<ul>
6+
<li>The name should map to the URL.</li>
7+
<li>No distro or app name prefix, they are namespaced by their dirs already</li>
8+
<li>Since templates are made in python, the are <code>named_with_underscores.html</code> (not web style dashes).</li>
9+
</ul>
10+
11+
<h2>URL PARAMETERS IN THE TEMPLATE</h2>
12+
13+
<ul>
14+
<li>All views (except <code>generic.View</code>) from <code>django.forms.generic</code> inherit from <code>ContextMixin</code></li>
15+
<li><p><code>ContextMixin</code> defines the method <code>get_context_data</code>:</p>
16+
17+
<div class="codehilite"><pre><span></span><code> <span class="k">def</span> <span class="nf">get_context_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
18+
<span class="n">kwargs</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">&#39;view&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>
19+
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">extra_context</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
20+
<span class="n">kwargs</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">extra_context</span><span class="p">)</span>
21+
<span class="k">return</span> <span class="n">kwargs</span>
22+
</code></pre></div>
23+
24+
<p>So when overriding one must be careful to extends <code>super</code>'s <code>kwargs</code>:</p>
25+
26+
<div class="codehilite"><pre><span></span><code> <span class="k">def</span> <span class="nf">get_context_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
27+
<span class="n">kwargs</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_context_data</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
28+
<span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;page_title&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;Documentation&quot;</span>
29+
<span class="k">return</span> <span class="n">kwargs</span>
30+
</code></pre></div></li>
31+
</ul>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"extras": ["fenced-code-blocks", "pygments"]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extra fenced-code-blocks pygments
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Django Templates
2+
3+
## NOTES
4+
5+
- The name should map to the URL.
6+
- No distro or app name prefix, they are namespaced by their dirs already
7+
- Since templates are made in python, the are `named_with_underscores.html` (not web style dashes).
8+
9+
## URL PARAMETERS IN THE TEMPLATE
10+
11+
- All views (except `generic.View`) from `django.forms.generic` inherit from `ContextMixin`
12+
- `ContextMixin` defines the method `get_context_data`:
13+
```python
14+
def get_context_data(self, **kwargs):
15+
kwargs.setdefault('view', self)
16+
if self.extra_context is not None:
17+
kwargs.update(self.extra_context)
18+
return kwargs
19+
```
20+
So when overriding one must be careful to extends `super`'s `kwargs`:
21+
```py
22+
def get_context_data(self, **kwargs):
23+
kwargs = super().get_context_data(**kwargs)
24+
kwargs['page_title'] = "Documentation"
25+
return kwargs
26+
```

0 commit comments

Comments
 (0)