Skip to content

Instantly share code, notes, and snippets.

@gustavofonseca
Created February 16, 2016 16:09
Show Gist options
  • Save gustavofonseca/79dd2495803972d6149e to your computer and use it in GitHub Desktop.
Save gustavofonseca/79dd2495803972d6149e to your computer and use it in GitHub Desktop.
Script para o desmembramento do arquivo ``tagset.rst`` em diversos outros arquivos ``.rst``.
#!/usr/bin/env python3
# coding: utf-8
"""
Script para o desmembramento do arquivo ``tagset.rst`` em diversos outros
arquivos ``.rst``. O critério para o desmembramento será o marcador de seção
descrito pela regex ``^\.\. _(?P<secname>[a-zA-Z0-9_-]+):$``
"""
import re
import itertools
FILENAME = 'tagset.rst'
SECNAME_PATTERN = re.compile(r'^\.\. _(?P<secname>[a-zA-Z0-9_-]+):$')
def map_sections(filename):
# lista associativa na forma [['secname', 'initial line', 'final line'],]
SECTIONS_MAP = []
with open(filename, 'r') as tagset:
current_line = 0
for line in tagset:
current_line += 1
is_section_id = re.match(SECNAME_PATTERN, line)
if is_section_id:
# a cada nova seção é possível determinar o fim da anterior
if len(SECTIONS_MAP) > 0:
last_entry = SECTIONS_MAP[-1]
last_entry[2] = current_line - 1
secname = is_section_id.group('secname')
SECTIONS_MAP.append([secname, current_line, None])
else:
continue
last_entry = SECTIONS_MAP[-1]
last_entry[2] = current_line
return SECTIONS_MAP
sections = map_sections(FILENAME)
with open(FILENAME, 'r') as tagset:
tagset_lines_list = list(tagset)
for sec in sections:
start = sec[1] - 1
end = sec[2] -1
tagset_lines_slice = tagset_lines_list[start:end]
print('Writing:', sec[0], '[%s:%s]' % (start, end))
with open('tagset/' + sec[0] + '.rst', 'w') as outfile:
outfile.write(''.join(tagset_lines_slice))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment