Skip to content

Instantly share code, notes, and snippets.

@peio
Created January 2, 2012 13:12
Show Gist options
  • Select an option

  • Save peio/1550635 to your computer and use it in GitHub Desktop.

Select an option

Save peio/1550635 to your computer and use it in GitHub Desktop.
Annotate an EC Directive with MarkDown
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
'''
Structure of a legal act
http://publications.europa.eu/code/en/en-120000.htm
# Bulgarian
#chapter_bg = re.compile('(^ГЛАВА [IVXLCDM]+)\s+([А-Яа-я0-9 ]{1,}$)', re.M | re.U)
'''
import re
f = open('DIRECTIVE 96-9-EC Database protection.txt','r')
directive = f.read()
f.close()
# 2.1. Title
title = re.compile('^((DIRECTIVE|REGLAMENT|REGULATION).*)')
# 2.2. Preamble - Citations and recitals
recitals = re.compile('^(\(\d+\) )', re.M)
have_adopted = re.compile('(HAVE ADOPTED THIS DIRECTIVE:)')
# 2.3. Articles (enacting terms)
chapter = re.compile('(^CHAPTER [IVXLCDM]+)\s+([\w ]{1,}$)', re.M)
article = re.compile('(^Article \d+)\s+([\w ]{1,}$)', re.M) # search m.group(1) ; m.group(2) ; article.sub(r'\1 \2', directive)
points = re.compile('(^\d{1,2}\.)', re.M)
letters = re.compile('(^\([a-z]\) )', re.M)
definitions = re.compile('\'([\w -]+)\` shall mean (.*)$', re.M)
#2.4. Compulsory character of regulations (concluding formula)
# 2.1. Title http://publications.europa.eu/code/en/en-120100.htm
directive = title.sub(r'# \1 \n\n[TOC]', directive)
# 2.2. Preamble - Citations and recitals
directive = recitals.sub(r'* \1 ', directive)
directive = have_adopted.sub(r'**\1**\n\n## Definitions:\n[Definitions]', directive)
# 2.3. Articles (enacting terms)
directive = chapter.sub(r'## \1 \2', directive)
directive = article.sub(r'### \1 \2', directive)
directive = points.sub(r'**\1** ', directive)
directive = letters.sub(r'* \1 ', directive)
DEFINITIONS = '\n'
FOOTNOTES = '\n'
for (term, definition) in definitions.findall(directive):
DEFINITIONS += '**'+term+'**'+' \n: '+definition+"\n"
FOOTNOTES += '[^'+term+']: '+'**'+term+'**'+' is '+definition+"\n"
''' VERY GENEROUS: directive = directive.replace(term, term+'[^'+term+']') '''
# Add Definition section
directive = directive.replace('[Definitions]', DEFINITIONS)
# Add Footnotes
directive += '\n# Footnotes:\n'+FOOTNOTES
directive = definitions.sub(r'\1[^\1] shall mean \2', directive)
#print FOOTNOTES
#print directive
# Save
f = open('DIRECTIVE 96-9-EC Database protection.md', 'w+')
f.write(directive)
f.close()
print 'Wrote to:'+'DIRECTIVE 96-9-EC Database protection.md'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment