output_engine = mustache("markdown")

section_regexps = [
    ('New', [
        r'^NEW(:*) \s*?([^\n]*)$',
     ]),
    ('Changes', [
        r'^CHANGE(D*)(:*) \s*?([^\n]*)$',
     ]),
    ('Fixes', [
        r'^FIX(:*) \s*?([^\n]*)$',
     ]),
    ('Optimizations', [
        r'^OPT(:*) \s*?([^\n]*)$',
     ]),
    ('Other', None ## Match all lines
     ),

]


ignore_regexps = [
    r'^Merge pull request',
    r'^Automatic merge',
]

tag_filter_regexp = r'^[0-9]+\.[0-9]+\.[0-9]+$'


OUTPUT_FILE = "CHANGELOG.md"
INSERT_POINT_REGEX = r'''(?isxu)
^
(
  \s*\#\s+Changelog\s*(\n|\r\n|\r)        ## ``Changelog`` line
)

(                     ## Match all between changelog and release rev
    (
      (?!
         (?<=(\n|\r))                ## look back for newline
         \#\#\s+%(rev)s                     ## revision
         \s+
         \([0-9]+-[0-9]{2}-[0-9]{2}\)(\n|\r\n|\r)   ## date
      )
      .
    )*
)

(?P<tail>\#\#\s+(?P<rev>%(rev)s))
''' % {'rev': r"[0-9]+\.[0-9]+(\.[0-9]+)?"}

revs = [
    Caret(FileFirstRegexMatch(OUTPUT_FILE, INSERT_POINT_REGEX)),
    "HEAD"
]

publish = FileRegexSubst(OUTPUT_FILE, INSERT_POINT_REGEX, r"\1\o\n\g<tail>")
