Skip to content

Instantly share code, notes, and snippets.

@MarkusH
Forked from pydanny/hackychecktitlecase.py
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save MarkusH/838f07e540faaf9ec528 to your computer and use it in GitHub Desktop.

Select an option

Save MarkusH/838f07e540faaf9ec528 to your computer and use it in GitHub Desktop.

Revisions

  1. MarkusH renamed this gist Apr 28, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. MarkusH revised this gist Apr 28, 2015. 1 changed file with 14 additions and 1 deletion.
    15 changes: 14 additions & 1 deletion hackychecktitlecase.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,19 @@
    #!/usr/bin/env python3
    """
    From @pydanny at https://gist.github.com/pydanny/ddf965bbec415084fb6d
    Git Integration
    ===============
    1. Move .git/hooks/pre-commit.sample to .git/hooks/pre-commit
    2. Add::
    ./hackychecktitlecase.py
    exit $?
    after the leading comments to prevent commits of "invalid" headlines.
    """

    import glob
    @@ -76,4 +89,4 @@ def main():


    if __name__ == "__main__":
    sys.exit(main())
    sys.exit(main())
  3. MarkusH revised this gist Apr 28, 2015. 1 changed file with 19 additions and 21 deletions.
    40 changes: 19 additions & 21 deletions hackychecktitlecase.py
    Original file line number Diff line number Diff line change
    @@ -7,6 +7,8 @@
    import re
    import sys

    from collections import defaultdict

    try:
    from titlecase import titlecase
    except ImportError:
    @@ -39,15 +41,12 @@ def clean_line(line):


    def main():
    needs_fixes = False
    fixes = defaultdict(list)
    for file in glob.glob("content/*.tex"):

    with open(file) as f:
    lines = f.readlines()

    print('=' * 79)
    print(file)

    for lineno, line in enumerate(lines, 1):
    match = re.match(regex, line)
    if match is None:
    @@ -57,25 +56,24 @@ def main():
    continue
    new_line = titlecase(line)
    if new_line != line:
    needs_fixes = True
    fixes[file].append((lineno, line, new_line))

    if fixes:
    print('There are titlecase errors')
    print('=' * 79)
    for file, changes in fixes.items():
    print(file)
    for lineno, line, new_line in changes:
    print('-' * 79)
    print('Line %d' % lineno)
    print('OLD: %s' % line)
    print('NEW: %s' % new_line)
    print('=' * 79)
    print('#: %d' % lineno)
    print('-: %s' % line)
    print('+: %s' % new_line)
    print('=' * 79)
    return 1

    return needs_fixes
    print('No titlecase errors :)')
    return 0


    if __name__ == "__main__":
    sys.exit(main())



    ### Git Integration:
    # 1. Move .git/hooks/pre-commit.sample to .git/hooks/pre-commit
    # 2. Add:
    # ./hackychecktitlecase.py
    # exit $?
    # after the leading comments to prevent commits of "invalid" headlines.
    ###
    sys.exit(main())
  4. MarkusH revised this gist Apr 27, 2015. 1 changed file with 39 additions and 16 deletions.
    55 changes: 39 additions & 16 deletions hackychecktitlecase.py
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,22 @@
    #!/usr/bin/env python3
    """
    From @pydanny at https://gist.github.com/pydanny/ddf965bbec415084fb6d
    """

    import glob
    import re
    from os.path import join
    import os
    import shutil
    import sys

    try:
    from titlecase import titlecase
    except ImportError:
    print "Please install titlecase"
    print("Please install titlecase")

    regex = re.compile(r'\\(chapter|section|subsection)', re.IGNORECASE)
    regex = re.compile(r'\\(section|subsection|subsubsection)', re.IGNORECASE)

    EXEMPTS = ()


    def exempter(line):
    for exempt in EXEMPTS:
    if line.startswith(exempt):
    @@ -21,9 +25,12 @@ def exempter(line):


    def clean_line(line):
    line = line.replace('\\chapter{', '')
    line = line.replace('\\section{', '')
    line = line.replace('\\subsection{', '')
    line = line.replace('\\subsubsection{', '')
    line = line.replace('\\section*{', '')
    line = line.replace('\\subsection*{', '')
    line = line.replace('\\subsubsection*{', '')
    line = line.replace('}', '')
    line = line.replace('[', '')
    line = line.replace(']', ' ')
    @@ -32,15 +39,16 @@ def clean_line(line):


    def main():
    needs_fixes = False
    for file in glob.glob("content/*.tex"):

    for chaptername in glob.glob("chapters/[0-9][0-9]*.tex"):

    chap_number = chaptername[9:11]

    with open(chaptername) as f:
    with open(file) as f:
    lines = f.readlines()

    for line in lines:
    print('=' * 79)
    print(file)

    for lineno, line in enumerate(lines, 1):
    match = re.match(regex, line)
    if match is None:
    continue
    @@ -49,10 +57,25 @@ def main():
    continue
    new_line = titlecase(line)
    if new_line != line:
    print "OLD: ", line
    print "NEW: ", new_line
    print "====================="
    needs_fixes = True
    print('-' * 79)
    print('Line %d' % lineno)
    print('OLD: %s' % line)
    print('NEW: %s' % new_line)
    print('=' * 79)

    return needs_fixes


    if __name__ == "__main__":
    main()
    sys.exit(main())



    ### Git Integration:
    # 1. Move .git/hooks/pre-commit.sample to .git/hooks/pre-commit
    # 2. Add:
    # ./hackychecktitlecase.py
    # exit $?
    # after the leading comments to prevent commits of "invalid" headlines.
    ###
  5. @pydanny pydanny created this gist Apr 25, 2015.
    58 changes: 58 additions & 0 deletions hackychecktitlecase.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    import glob
    import re
    from os.path import join
    import os
    import shutil

    try:
    from titlecase import titlecase
    except ImportError:
    print "Please install titlecase"

    regex = re.compile(r'\\(chapter|section|subsection)', re.IGNORECASE)

    EXEMPTS = ()

    def exempter(line):
    for exempt in EXEMPTS:
    if line.startswith(exempt):
    return True
    return line in EXEMPTS


    def clean_line(line):
    line = line.replace('\\chapter{', '')
    line = line.replace('\\section{', '')
    line = line.replace('\\subsection{', '')
    line = line.replace('}', '')
    line = line.replace('[', '')
    line = line.replace(']', ' ')
    line = line.strip()
    return line


    def main():

    for chaptername in glob.glob("chapters/[0-9][0-9]*.tex"):

    chap_number = chaptername[9:11]

    with open(chaptername) as f:
    lines = f.readlines()

    for line in lines:
    match = re.match(regex, line)
    if match is None:
    continue
    line = clean_line(line)
    if exempter(line):
    continue
    new_line = titlecase(line)
    if new_line != line:
    print "OLD: ", line
    print "NEW: ", new_line
    print "====================="


    if __name__ == "__main__":
    main()