mirror of
https://github.com/gryf/ebook-converter.git
synced 2026-04-16 02:53:33 +02:00
Clean up titlecase module
This commit is contained in:
@@ -6,11 +6,6 @@ License: http://www.opensource.org/licenses/mit-license.php
|
|||||||
"""
|
"""
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from ebook_converter.utils.icu import capitalize, upper
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['titlecase']
|
|
||||||
__version__ = '0.5'
|
|
||||||
|
|
||||||
SMALL = 'a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v\\.?|via|vs\\.?'
|
SMALL = 'a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v\\.?|via|vs\\.?'
|
||||||
PUNCT = r"""!"#$%&'‘’()*+,\-‒–—―./:;?@[\\\]_`{|}~"""
|
PUNCT = r"""!"#$%&'‘’()*+,\-‒–—―./:;?@[\\\]_`{|}~"""
|
||||||
@@ -26,16 +21,6 @@ SUBPHRASE = re.compile(r'([:.;?!][ ])(%s)' % SMALL)
|
|||||||
APOS_SECOND = re.compile(r"^[dol]{1}['‘]{1}[a-z]+$", re.I)
|
APOS_SECOND = re.compile(r"^[dol]{1}['‘]{1}[a-z]+$", re.I)
|
||||||
UC_INITIALS = re.compile(r"^(?:[A-Z]{1}\.{1}|[A-Z]{1}\.{1}[A-Z]{1})+$")
|
UC_INITIALS = re.compile(r"^(?:[A-Z]{1}\.{1}|[A-Z]{1}\.{1}[A-Z]{1})+$")
|
||||||
|
|
||||||
_lang = None
|
|
||||||
|
|
||||||
|
|
||||||
def lang():
|
|
||||||
global _lang
|
|
||||||
if _lang is None:
|
|
||||||
from ebook_converter.utils.localization import get_lang
|
|
||||||
_lang = get_lang().lower()
|
|
||||||
return _lang
|
|
||||||
|
|
||||||
|
|
||||||
def titlecase(text):
|
def titlecase(text):
|
||||||
"""
|
"""
|
||||||
@@ -49,7 +34,7 @@ def titlecase(text):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
all_caps = upper(text) == text
|
all_caps = text.upper() == text
|
||||||
|
|
||||||
pat = re.compile(r'(\s+)')
|
pat = re.compile(r'(\s+)')
|
||||||
line = []
|
line = []
|
||||||
@@ -85,20 +70,18 @@ def titlecase(text):
|
|||||||
|
|
||||||
result = "".join(line)
|
result = "".join(line)
|
||||||
|
|
||||||
result = SMALL_FIRST.sub(lambda m: '%s%s' % (
|
result = SMALL_FIRST.sub(lambda m: '%s%s' % (m.group(1),
|
||||||
m.group(1),
|
m.group(2).capitalize()),
|
||||||
capitalize(m.group(2))
|
result)
|
||||||
), result)
|
|
||||||
|
|
||||||
result = SMALL_AFTER_NUM.sub(lambda m: '%s%s' % (m.group(1),
|
result = SMALL_AFTER_NUM.sub(lambda m: '%s%s' % (m.group(1),
|
||||||
capitalize(m.group(2))
|
m.group(2).capitalize()),
|
||||||
), result)
|
result)
|
||||||
|
|
||||||
result = SMALL_LAST.sub(lambda m: capitalize(m.group(0)), result)
|
result = SMALL_LAST.sub(lambda m: m.group(0).capitalize(), result)
|
||||||
|
|
||||||
result = SUBPHRASE.sub(lambda m: '%s%s' % (
|
result = SUBPHRASE.sub(lambda m: '%s%s' % (m.group(1),
|
||||||
m.group(1),
|
m.group(2).capitalize()),
|
||||||
capitalize(m.group(2))
|
result)
|
||||||
), result)
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user