From 1d3866a30eea88ff99d4aceda53c4871f61c11c0 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Tue, 11 Aug 2020 08:48:25 +0200 Subject: content: Add to_camel_case() --- rtemsspec/content.py | 11 +++++++++++ rtemsspec/sphinxcontent.py | 11 ++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/rtemsspec/content.py b/rtemsspec/content.py index 74b2b43e..3c28ab66 100644 --- a/rtemsspec/content.py +++ b/rtemsspec/content.py @@ -1031,3 +1031,14 @@ def enabled_by_to_exp(enabled_by: Any, mapper: ExpressionMapper) -> str: if exp.startswith("("): return exp[1:-1] return exp + + +_CAMEL_CASE_TO_UPPER = re.compile(r"\s+(.)") +_CAMEL_CASE_DISCARD = re.compile(r"[^ \t\n\r\f\va-zA-Z0-9]") + + +def to_camel_case(name: str) -> str: + """ Returns the name in CamelCase. """ + return name[0].upper() + _CAMEL_CASE_TO_UPPER.sub( + lambda match: match.group(1).upper(), + _CAMEL_CASE_DISCARD.sub(" ", name[1:].replace("+", "X"))) diff --git a/rtemsspec/sphinxcontent.py b/rtemsspec/sphinxcontent.py index 85c7858c..914e2436 100644 --- a/rtemsspec/sphinxcontent.py +++ b/rtemsspec/sphinxcontent.py @@ -25,10 +25,9 @@ # POSSIBILITY OF SUCH DAMAGE. from contextlib import contextmanager -import re from typing import Any, Iterable, Iterator, List, Optional, Union -from rtemsspec.content import Content, make_lines +from rtemsspec.content import Content, make_lines, to_camel_case from rtemsspec.items import Item, ItemGetValueContext, ItemMapper GenericContent = Union[str, List[str], "Content"] @@ -38,12 +37,6 @@ GenericContentIterable = Union[Iterable[str], Iterable[List[str]], _HEADER_LEVELS = ["#", "*", "=", "-", "^", "\""] -def _to_camel_case(name: str) -> str: - return name[0].upper() + re.sub( - r"\s+(.)", lambda match: match.group(1).upper(), - re.sub(r"[^ \t\n\r\f\va-zA-Z0-9]", " ", name[1:].replace("+", "X"))) - - def get_reference(label: str, name: Optional[str] = None) -> str: """ Returns the reference to the specified label. """ if name: @@ -53,7 +46,7 @@ def get_reference(label: str, name: Optional[str] = None) -> str: def get_label(name: str) -> str: """ Returns the label for the specified name. """ - return _to_camel_case(name.strip()) + return to_camel_case(name.strip()) class SphinxContent(Content): -- cgit v1.2.3