-
Notifications
You must be signed in to change notification settings - Fork 0
/
changecase.py
78 lines (71 loc) · 2.38 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import re, 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()
always_uppercase = r'''\bXML|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(titlecase_plus(text), {'"': '"', '\n': ' '} ).replace(' ', ''),
'kebab': escape(text.lower(), {'"': '"', '\n': ' '} ).replace(' ', '-').replace('_', '-'),
'snake': escape(text.lower(), {'"': '"', '\n': ' '} ).replace(' ', '_').replace('-', '_')
}
print """{
"items": [
{
"title": "%(lower)s",
"arg": "%(lower)s",
"subtitle": "lowercase",
"icon": "lowercase.png"
},
{
"title": "%(upper)s",
"arg": "%(upper)s",
"subtitle": "UPPERCASE",
"icon": "uppercase.png"
},
{
"title": "%(title)s",
"arg": "%(title)s",
"subtitle": "Title Case",
"icon": "titlecase.png"
},
{
"title": "%(camel)s",
"arg": "%(camel)s",
"subtitle": "CamelCase",
"png": "camelcase.png"
},
{
"title": "%(kebab)s",
"arg": "%(kebab)s",
"subtitle": "kab-case",
"png": "kebabcase.png"
},
{
"title": "%(snake)s",
"arg": "%(snake)s",
"subtitle": "snake_case",
"png": "snakecase.png"
}
]
}""" % variations