Skip to content

Commit 5741e58

Browse files
author
Raniere Silva
committed
Merge pull request #37 from aaren/prereq-divs
Prereq divs
2 parents bc06c58 + 2da5e1c commit 5741e58

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ file, but again, we're trying to avoid those.
346346
---
347347
Paragraph(s) of introductory material.
348348
349-
> ## Prerequisites {.prereq}
349+
> ## Prerequisites
350350
>
351351
> What learners need to know before tackling this lesson.
352352

pages/01-one.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Lesson Title
44
subtitle: Topic Title One
55
minutes: 10
66
---
7-
> ## Learning Objectives {.objectives}
7+
> ## Learning Objectives
88
>
99
> * Learning objective 1
1010
> * Learning objective 2

pages/02-two.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Lesson Title
44
subtitle: Topic Title Two
55
minutes: 10
66
---
7-
> ## Learning Objectives {.objectives}
7+
> ## Learning Objectives
88
>
99
> * Learning objective 1
1010
> * Learning objective 2

tools/blockquote2div.py

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
99
A blockquote will be converted if
1010
11-
1. it begins with a header
12-
2. that header has attributes
13-
3. those attributes contain a single class
14-
4. that class is one of ['objectives', 'callout', 'challenge']
11+
1. it begins with a header
12+
2. that either
13+
1. matches "Prerequisites", "Objectives", "Callout" or "Challenge" OR
14+
2. has attributes containing a single class matching
15+
one of ['prereq', 'objectives', 'callout', 'challenge']
1516
1617
For example, this is a valid blockquote:
1718
@@ -25,6 +26,18 @@
2526
Let's do something.
2627
</div>
2728
29+
This is also a valid blockquote:
30+
31+
> ## Prerequisites
32+
> Breakfast!
33+
34+
and it will be converted into this markdown:
35+
36+
<div class='prereq'>
37+
## Prerequisites
38+
Breakfast!
39+
</div>
40+
2841
2942
For debugging purposes you may find it useful to test the filter
3043
like this:
@@ -34,10 +47,21 @@
3447
import pandocfilters as pf
3548

3649

37-
valid_classes = ['objectives', 'callout', 'challenge']
50+
# These are classes that, if set on the title of a blockquote, will
51+
# trigger the blockquote to be converted to a div.
52+
SPECIAL_CLASSES = ['callout', 'challenge', 'prereq', 'objectives']
3853

54+
# These are titles of blockquotes that will cause the blockquote to
55+
# be converted into a div. They are 'title': 'class' pairs, where the
56+
# 'title' will create a blockquote with the corresponding 'class'.
57+
SPECIAL_TITLES = {'prerequisites': 'prereq',
58+
'learning objectives': 'objectives',
59+
'objectives': 'objectives',
60+
'challenge': 'challenge',
61+
'callout': 'callout'}
3962

40-
def find_attributes(blockquote):
63+
64+
def find_header(blockquote):
4165
"""Find attributes in a blockquote if they are defined on a
4266
header that is the first thing in the block quote.
4367
@@ -46,7 +70,7 @@ def find_attributes(blockquote):
4670
"""
4771
if blockquote[0]['t'] == 'Header':
4872
level, attr, inline = blockquote[0]['c']
49-
return attr
73+
return level, attr, inline
5074

5175

5276
def remove_attributes(blockquote):
@@ -71,10 +95,21 @@ def blockquote2div(key, value, format, meta):
7195
"""
7296
if key == 'BlockQuote':
7397
blockquote = value
74-
attr = find_attributes(blockquote)
75-
if not attr:
98+
99+
header = find_header(blockquote)
100+
if not header:
76101
return
77-
elif len(attr[1]) == 1 and attr[1][0] in valid_classes:
102+
else:
103+
level, attr, inlines = header
104+
105+
id, classes, kvs = attr
106+
107+
ltitle = pf.stringify(inlines).lower()
108+
if ltitle in SPECIAL_TITLES:
109+
classes.append(SPECIAL_TITLES[ltitle])
110+
return pf.Div(attr, blockquote)
111+
112+
elif len(classes) == 1 and classes[0] in SPECIAL_CLASSES:
78113
remove_attributes(blockquote)
79114
# a blockquote is just a list of blocks, so it can be
80115
# passed directly to Div, which expects Div(attr, blocks)

0 commit comments

Comments
 (0)