-
Notifications
You must be signed in to change notification settings - Fork 12
/
changecase.py
79 lines (68 loc) · 2.47 KB
/
changecase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
from __future__ import print_function
import re
import sys
from xml.sax.saxutils import escape
from titlecase import titlecase
if len(sys.argv) > 1 and len(sys.argv[1].strip()):
text = sys.argv[1]
else:
text = sys.stdin.read()
if len(text.strip()) == 0:
sys.exit(0)
always_uppercase = r'''\b(?:XML|HTML|CSS|JSON|FYI|AOL|ATM|BBC|CD|FAQ|GAIM|GNU|GTK|HIRD|HIV
|HURD|IEEE|IOU|IRA|IUPAC|JPEG|LCD|NAACP|NAC|NATO|NCAA|NOAD|OEM|PHP|ROM|SAT|SFMOMA|SQL|USA|VHDL|VHSIC|W3C
|LOL|WTF)\b'''
always_uppercase_re = re.compile(always_uppercase, re.I | re.X)
def titlecase_plus(text):
"""The titlecase module assumes words in all UPPERCASE should be ignored.
This works for words like HTML, FYI, ID, etc., but not generally. Just work
around for now by going to .lower first. Then, replace any well known
"always" uppercase"""
text = titlecase(text.lower())
def upcase(m):
return m.group().upper()
return always_uppercase_re.sub(upcase, text)
variations = {
'lower': escape(text.lower(), {'"': '"', '\n': ' '}),
'upper': escape(text.upper(), {'"': '"', '\n': ' '}),
'title': escape(titlecase_plus(text), {'"': '"', '\n': ' '}),
'camel': escape(text.title(), {'"': '"', '\n': ' '}).replace(' ', ''),
'kebab': escape(text.lower(), {'"': '"', '\n': ' '}).replace(' ', '-').replace('_', '-'),
'snake': escape(text.lower(), {'"': '"', '\n': ' '}).replace(' ', '_').replace('-', '_')
}
print("""<?xml version="1.0"?>
<items>
<item arg="%(lower)s">
<title>%(lower)s</title>
<subtitle>Transform text to `lowercase`</subtitle>
<icon>lowercase.png</icon>
</item>
<item arg="%(upper)s">
<title>%(upper)s</title>
<subtitle>Transform text to `UPPERCASE`</subtitle>
<icon>uppercase.png</icon>
</item>
<item arg="%(title)s">
<title>%(title)s</title>
<subtitle>Transform text to `Title Case`</subtitle>
<icon>titlecase.png</icon>
</item>
<item arg="%(camel)s">
<title>%(camel)s</title>
<subtitle>Transform text to `CamelCase`</subtitle>
<icon>camelcase.png</icon>
</item>
<item arg="%(kebab)s">
<title>%(kebab)s</title>
<subtitle>Transform text to hyphenated `kebab-case`</subtitle>
<icon>kebabcase.png</icon>
</item>
<item arg="%(snake)s">
<title>%(snake)s</title>
<subtitle>Transform text to `snake_case`</subtitle>
<icon>snakecase.png</icon>
</item>
</items>""" % variations)