Skip to content

Commit e847366

Browse files
Merge pull request #194 from uccser/issue/193
Clear IDs only when requested.
2 parents 1cd475d + 6eb69b2 commit e847366

File tree

7 files changed

+153
-11
lines changed

7 files changed

+153
-11
lines changed

docs/source/usage.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ The following attributes are available:
116116
[]
117117
}
118118
119+
(Optional) Step 5: Clearing Saved Data
120+
=======================================
121+
122+
Lastly there is some data that is saved between conversions such as ``required_files`` and unique ids used in the ``glossary`` and for ``headings``. This can be cleared by using the following method:
123+
124+
.. code-block:: python
125+
126+
converter.clear_saved_data()
127+
119128
Configuring Verto converter after creation
120129
===============================================
121130

@@ -155,7 +164,7 @@ Full list of package methods
155164
=======================================
156165

157166
.. autoclass:: verto.Verto()
158-
:members: __init__, convert, update_processors, processor_defaults, update_templates, clear_templates
167+
:members: __init__, convert, update_processors, processor_defaults, update_templates, clear_templates, clear_saved_data
159168

160169
.. autoclass:: verto.Verto.VertoResult()
161170

@@ -170,3 +179,11 @@ Full list of package methods
170179
.. attribute:: required_files
171180

172181
A dictionary of files encountered in a Verto conversion. The dictionary has a string for the file type as the key (for example: ``image``) and a set of all file paths encountered as the value (for example: ``{'image/face.png', 'image/logo.png`}``).
182+
183+
.. attribute:: heading_tree
184+
185+
A tuple of namedtuples which describes the tree of headings, as generated by our heading processor. Each namedtuple contains a title (string), title_slug (string), level (integer) and children (tuple of nodes).
186+
187+
.. attribute:: required_glossary_terms
188+
189+
A dictionary of terms to a list of tuples containing reference text and link IDs.

verto/Verto.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def convert(self, text):
6666
Returns:
6767
A VertoResult object.
6868
'''
69-
self.verto_extension.clear_saved_data()
69+
self.verto_extension.clear_document_data()
7070
html_string = self.converter.convert(text)
7171
result = VertoResult(
7272
html_string=html_string,
@@ -77,6 +77,12 @@ def convert(self, text):
7777
)
7878
return result
7979

80+
def clear_saved_data(self):
81+
'''Clears data that is saved between documents. This should be
82+
called between conversions on unrelated documents.
83+
'''
84+
self.verto_extension.clear_saved_data()
85+
8086
def update_templates(self, html_templates):
8187
'''Update the template dictionary with the given dictionary
8288
of templates, while leaving all other HTML templates (including

verto/VertoExtension.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,14 @@ def __init__(self, processors=[], html_templates={}, extensions=[], *args, **kwa
5656
extensions: A list of extra extensions for compatibility.
5757
'''
5858
super().__init__(*args, **kwargs)
59-
self.required_files = defaultdict(set)
60-
self.title = None
6159
self.jinja_templates = self.loadJinjaTemplates(html_templates)
6260
self.processor_info = self.loadProcessorInfo()
6361
self.processors = processors
62+
self.title = None
63+
self.heading_tree = None
6464
self.custom_slugify = UniqueSlugify()
6565
self.glossary_terms = defaultdict(list)
66-
self.heading_tree = None
67-
66+
self.required_files = defaultdict(set)
6867
self.compatibility = []
6968
for extension in extensions:
7069
if isinstance(extension, utils.string_type):
@@ -114,13 +113,18 @@ def update_processors(processors, markdown_processors):
114113
processor = ScratchCompatibilityPreprocessor(self, md)
115114
md.preprocessors.add('scratch-compatibility', processor, '<fenced_code_block')
116115

116+
def clear_document_data(self):
117+
'''Clears information stored for a specific document.
118+
'''
119+
self.title = None
120+
self.heading_tree = None
121+
117122
def clear_saved_data(self):
118123
'''Clears stored information from processors, should be called
119-
between runs.
124+
between runs on unrelated documents.
120125
'''
121-
self.title = None
122126
self.custom_slugify.clear()
123-
self.heading_tree = None
127+
self.glossary_terms.clear()
124128
for key in self.required_files.keys():
125129
self.required_files[key].clear()
126130

verto/processors/HeadingBlockProcessor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ def run(self, parent, blocks):
5252
'''
5353
block = blocks.pop(0)
5454
match = self.pattern.search(block)
55-
assert match is not None # If this is true how did we test successfully
5655

5756
before = block[:match.start()]
5857
after = block[match.end():]
@@ -91,6 +90,7 @@ def add_to_heading_tree(self, heading, heading_slug, level):
9190
level: the level of the heading
9291
'''
9392
if self.get_ext_tree() is None: # We are likely on a new file
93+
self.roots = []
9494
self.current_node = None
9595

9696
# Who is our parent node

verto/tests/ConfigurationTest.py

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,108 @@ def test_multiple_calls(self):
129129
)
130130
]
131131

132+
verto = Verto()
132133
for filename, expected_result in test_cases:
133-
verto = Verto()
134134
test_string = self.read_test_file(self.test_name, filename)
135135
verto_result = verto.convert(test_string)
136136

137137
self.assertEqual(verto_result.title, expected_result.title)
138138
self.assertEqual(verto_result.required_files, expected_result.required_files)
139139
self.assertTupleEqual(verto_result.heading_tree, expected_result.heading_tree)
140140
self.assertDictEqual(verto_result.required_glossary_terms, expected_result.required_glossary_terms)
141+
verto.clear_saved_data()
142+
143+
def test_multiple_calls_without_clearing(self):
144+
'''Tests that if the verto extension is not cleared that
145+
information such as required_files and slugs are persistant.
146+
'''
147+
filename = 'all_processors.md'
148+
other_filename = 'otherfile.md'
149+
expected_result = VertoResult(
150+
html_string=self.read_test_file(self.test_name, 'all_processors_expected.html', strip=True),
151+
title='Example Title',
152+
required_files={
153+
'interactives': {
154+
'binary-cards'
155+
},
156+
'images': set(),
157+
'page_scripts': set(),
158+
'scratch_images': {
159+
ScratchImageMetaData(
160+
hash='a0f8fcad796864abfacac8bda6e0719813833fd1fca348700abbd040557c1576',
161+
text='when flag clicked\nclear\nforever\npen down\nif <<mouse down?> and <touching [mouse-pointer v]?>> then\nswitch costume to [button v]\nelse\nadd (x position) to [list v]\nend\nmove (foo) steps\nturn ccw (9) degrees'
162+
),
163+
}
164+
},
165+
heading_tree=(HeadingNode(
166+
title='Example Title',
167+
title_slug='example-title',
168+
level=1,
169+
children=(),
170+
),
171+
HeadingNode(
172+
title='Example Title 2',
173+
title_slug='example-title-2',
174+
level=1,
175+
children=()
176+
),
177+
),
178+
required_glossary_terms={
179+
'algorithm': []
180+
}
181+
)
182+
expected_otherfile_result = VertoResult(
183+
html_string=self.read_test_file(self.test_name, 'otherfile_expected.html', strip=True),
184+
title='Example Title',
185+
required_files={
186+
'interactives': {
187+
'binary-cards'
188+
},
189+
'images': {
190+
'pixel-diamond.png'
191+
},
192+
'page_scripts': set(),
193+
'scratch_images': {
194+
ScratchImageMetaData(
195+
hash='a0f8fcad796864abfacac8bda6e0719813833fd1fca348700abbd040557c1576',
196+
text='when flag clicked\nclear\nforever\npen down\nif <<mouse down?> and <touching [mouse-pointer v]?>> then\nswitch costume to [button v]\nelse\nadd (x position) to [list v]\nend\nmove (foo) steps\nturn ccw (9) degrees'
197+
),
198+
ScratchImageMetaData(
199+
hash='b78bff524e54a18116e1e898a93e360827f874a8b0b508e1edc47d21516495ad',
200+
text='never\ngoing\nto\ngive\nyou\nup'
201+
),
202+
}
203+
},
204+
heading_tree=(HeadingNode(
205+
title='Example Title',
206+
title_slug='example-title-3',
207+
level=1,
208+
children=(),
209+
),
210+
),
211+
required_glossary_terms={
212+
'algorithm': []
213+
}
214+
)
215+
216+
verto = Verto()
217+
# First file
218+
test_string = self.read_test_file(self.test_name, filename)
219+
verto_result = verto.convert(test_string)
220+
221+
self.assertEqual(verto_result.title, expected_result.title)
222+
self.assertEqual(verto_result.required_files, expected_result.required_files)
223+
self.assertTupleEqual(verto_result.heading_tree, expected_result.heading_tree)
224+
self.assertDictEqual(verto_result.required_glossary_terms, expected_result.required_glossary_terms)
225+
226+
# Another file
227+
test_string = self.read_test_file(self.test_name, other_filename)
228+
verto_result = verto.convert(test_string)
229+
230+
self.assertEqual(verto_result.title, expected_otherfile_result.title)
231+
self.assertEqual(verto_result.required_files, expected_otherfile_result.required_files)
232+
self.assertTupleEqual(verto_result.heading_tree, expected_otherfile_result.heading_tree)
233+
self.assertDictEqual(verto_result.required_glossary_terms, expected_otherfile_result.required_glossary_terms)
141234

142235
def test_custom_processors_and_custom_templates_on_creation(self):
143236
'''Checks if custom processors and custom templates work
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Example Title
2+
3+
{image file-path="pixel-diamond.png" alt="A diamond shape made out of pixels"}
4+
5+
scratch
6+
never
7+
going
8+
to
9+
give
10+
you
11+
up
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<h1 id="example-title">
2+
<span class="section_number">
3+
{{ chapter.number }}.
4+
</span>
5+
Example Title
6+
</h1>
7+
<div>
8+
<img alt="A diamond shape made out of pixels" class="" src="{% static 'pixel-diamond.png' %}" />
9+
</div>
10+
<object data="{% static 'scratch-blocks-b78bff524e54a18116e1e898a93e360827f874a8b0b508e1edc47d21516495ad.svg' %}" type="image/svg+xml">
11+
</object>

0 commit comments

Comments
 (0)