Skip to content

Commit

Permalink
initial commit: caja-rename 22.6.29 fork
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0w committed Aug 30, 2022
0 parents commit 1666041
Show file tree
Hide file tree
Showing 35 changed files with 4,203 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
project.geany
TODO
__pycache__/
674 changes: 674 additions & 0 deletions COPYING

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Caja Rename
An extension for Caja allowing users to rename multiple files/folders in a single pass. The application can change the case, insert, replace and delete strings, as well as enumerate the selection. Any changes are instantly visible in the preview list. The user interface strives to be as simple as possible, without confusing advanced operations.

For further info please see https://tari.in/www/software/caja-rename/
1 change: 1 addition & 0 deletions cajarename/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

36 changes: 36 additions & 0 deletions cajarename/appdata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# caja-rename.py
#
# Copyright 2017-2021 Robert Tari <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

APPNAME = 'cajarename'
APPEXECUTABLE='/usr/share/caja-python/extensions/caja-rename.py'
APPDEPENDENCIES = ['python3-gi', 'gir1.2-gtk-3.0', 'caja']
APPVERSION = '22.6.29'
APPSHOWSETTINGS = 170323
APPYEAR = '2017'
APPTITLE = 'Rename...'
APPDESCRIPTION = 'Batch renaming extension for Caja.'
APPLONGDESCRIPTION = 'An extension for Caja allowing users to rename multiple files/folders in a single pass. The application can change the case, insert, replace and delete strings, as well as enumerate the selection. Any changes are instantly visible in the preview list. The user interface strives to be as simple as possible, without confusing advanced operations.'
APPAUTHOR = 'Robert Tari'
APPMAIL = '[email protected]'
APPURL = 'https://tari.in/www/software/caja-rename/'
APPKEYWORDS = ['caja','batch','rename','python','extension']
APPDEBUG = []
179 changes: 179 additions & 0 deletions cajarename/titlecase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Original Perl version by: John Gruber http://daringfireball.net/ 10 May 2008
Python version by Stuart Colville http://muffinresearch.co.uk
License: http://www.opensource.org/licenses/mit-license.php
"""

import argparse
import re
import sys

__all__ = ['titlecase']
__version__ = '0.9.0'

SMALL = 'a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v\.?|via|vs\.?'
PUNCT = r"""!"#$%&'‘()*+,\-./:;?@[\\\]_`{|}~"""

SMALL_WORDS = re.compile(r'^(%s)$' % SMALL, re.I)
INLINE_PERIOD = re.compile(r'[a-z][.][a-z]', re.I)
UC_ELSEWHERE = re.compile(r'[%s]*?[a-zA-Z]+[A-Z]+?' % PUNCT)
CAPFIRST = re.compile(r"^[%s]*?([A-Za-z])" % PUNCT)
SMALL_FIRST = re.compile(r'^([%s]*)(%s)\b' % (PUNCT, SMALL), re.I)
SMALL_LAST = re.compile(r'\b(%s)[%s]?$' % (SMALL, PUNCT), re.I)
SUBPHRASE = re.compile(r'([:.;?!\-\—][ ])(%s)' % SMALL)
APOS_SECOND = re.compile(r"^[dol]{1}['‘]{1}[a-z]+(?:['s]{2})?$", re.I)
ALL_CAPS = re.compile(r'^[A-Z\s\d%s]+$' % PUNCT)
UC_INITIALS = re.compile(r"^(?:[A-Z]{1}\.{1}|[A-Z]{1}\.{1}[A-Z]{1})+$")
MAC_MC = re.compile(r"^([Mm]c|MC)(\w.+)")


def set_small_word_list(small=SMALL):
global SMALL_WORDS
global SMALL_FIRST
global SMALL_LAST
global SUBPHRASE
SMALL_WORDS = re.compile(r'^(%s)$' % small, re.I)
SMALL_FIRST = re.compile(r'^([%s]*)(%s)\b' % (PUNCT, small), re.I)
SMALL_LAST = re.compile(r'\b(%s)[%s]?$' % (small, PUNCT), re.I)
SUBPHRASE = re.compile(r'([:.;?!][ ])(%s)' % small)


def titlecase(text, callback=None, small_first_last=True):
"""
Titlecases input text
This filter changes all words to Title Caps, and attempts to be clever
about *un*capitalizing SMALL words like a/an/the in the input.
The list of "SMALL words" which are not capped comes from
the New York Times Manual of Style, plus 'vs' and 'v'.
"""

lines = re.split('[\r\n]+', text)
processed = []
for line in lines:
all_caps = ALL_CAPS.match(line)
words = re.split('[\t ]', line)
tc_line = []
for word in words:
if callback:
new_word = callback(word, all_caps=all_caps)
if new_word:
tc_line.append(new_word)
continue

if all_caps:
if UC_INITIALS.match(word):
tc_line.append(word)
continue

if APOS_SECOND.match(word):
if len(word[0]) == 1 and word[0] not in 'aeiouAEIOU':
word = word[0].lower() + word[1] + word[2].upper() + word[3:]
else:
word = word[0].upper() + word[1] + word[2].upper() + word[3:]
tc_line.append(word)
continue

match = MAC_MC.match(word)
if match:
tc_line.append("%s%s" % (match.group(1).capitalize(),
titlecase(match.group(2),callback,small_first_last)))
continue

if INLINE_PERIOD.search(word) or (not all_caps and UC_ELSEWHERE.match(word)):
tc_line.append(word)
continue
if SMALL_WORDS.match(word):
tc_line.append(word.lower())
continue

if "/" in word and "//" not in word:
slashed = map(
lambda t: titlecase(t,callback,False),
word.split('/')
)
tc_line.append("/".join(slashed))
continue

if '-' in word:
hyphenated = map(
lambda t: titlecase(t,callback,small_first_last),
word.split('-')
)
tc_line.append("-".join(hyphenated))
continue

if all_caps:
word = word.lower()

# Just a normal word that needs to be capitalized
tc_line.append(CAPFIRST.sub(lambda m: m.group(0).upper(), word))


result = " ".join(tc_line)

if small_first_last:
result = SMALL_FIRST.sub(lambda m: '%s%s' % (
m.group(1),
m.group(2).capitalize()
), result)

result = SMALL_LAST.sub(lambda m: m.group(0).capitalize(), result)

result = SUBPHRASE.sub(lambda m: '%s%s' % (
m.group(1),
m.group(2).capitalize()
), result)

processed.append(result)

return "\n".join(processed)


def cmd():
'''Handler for command line invocation'''

# Try to handle any reasonable thing thrown at this.
# Consume '-f' and '-o' as input/output, allow '-' for stdin/stdout
# and treat any subsequent arguments as a space separated string to
# be titlecased (so it still works if people forget quotes)
parser = argparse.ArgumentParser()
in_group = parser.add_mutually_exclusive_group()
in_group.add_argument('string', nargs='*', default=[],
help='String to titlecase')
in_group.add_argument('-f', '--input-file',
help='File to read from to titlecase')
parser.add_argument('-o', '--output-file',
help='File to write titlecased output to)')

args = parser.parse_args()

if args.input_file is not None:
if args.input_file == '-':
ifile = sys.stdin
else:
ifile = open(args.input_file)
else:
ifile = sys.stdin

if args.output_file is not None:
if args.output_file == '-':
ofile = sys.stdout
else:
ofile = open(args.output_file, 'w')
else:
ofile = sys.stdout

if len(args.string) > 0:
in_string = ' '.join(args.string)
else:
with ifile:
in_string = ifile.read()

with ofile:
ofile.write(titlecase(in_string))
Loading

0 comments on commit 1666041

Please sign in to comment.