def srt_to_audacity(data, force_endtime=False): """docstring for srt_to_audacity""" # FIXME: UnicodeDecodeError... pattern = re.compile(DATETIMECODE_HEADER_RE, re.I | re.M | re.X) stack = [] for t in spliterator(pattern, data, returnLeading=0): m = pattern.match(t[0]).groupdict() if force_endtime: if len(stack) and stack[-1]["end"] == "": stack[-1]["end"] = timecode_tosecs(m["start"]) end = timecode_tosecs(m["end"]) or "" else: end = timecode_tosecs(m["end"]) or timecode_tosecs(m["start"]) stack.append({"start": timecode_tosecs(m["start"]), "end": end, "body": t[1].strip("\n")}) template = "{e[start]}\t{e[end]}\t{e[body]}\n" return "".join([template.format(e=e) for e in stack])
def annotation_export(request, slug, section, _format="audacity", force_endtime=False): context = {} name = dewikify(slug) page = Page.objects.get(name=name) section = sectionalize(page.content)[int(section)] # TODO: Regex should not be defined more once within active archives TIME_RE = r'(\d\d:)?(\d\d):(\d\d)([,.]\d{1,3})?' TIMECODE_RE = r'(?P<start>%(TIME_RE)s)[ \t]*-->([ \t]*(?P<end>%(TIME_RE)s))?' % locals() OTHER_RE = r'.+' TIMECODE_HEADER_RE = r'^%(TIMECODE_RE)s(%(OTHER_RE)s)?$' % locals() pattern = re.compile(TIMECODE_HEADER_RE, re.I | re.M | re.X) stack = [] for t in spliterator(pattern, section['header'] + section['body']): m = pattern.match(t['header']).groupdict() if force_endtime: if len(stack) and stack[-1]['end'] == '': stack[-1]['end'] = timecode_tosecs(m['start']) end = timecode_tosecs(m['end']) or '' else: end = timecode_tosecs(m['end']) or timecode_tosecs(m['start']) stack.append({ 'start': timecode_tosecs(m['start']), 'end': end, 'body': t['body'].strip('\n'), }) context = {'sections': stack} return render_to_response("aawiki/annotation_export.audacity", context, context_instance=RequestContext(request), mimetype="text/plain;charset=utf-8")