summaryrefslogtreecommitdiff
path: root/htmtl/expression_modifiers/truncate.py
diff options
context:
space:
mode:
authorAsko Nõmm <asko@nmm.ee>2024-12-31 00:30:06 +0200
committerAsko Nõmm <asko@nmm.ee>2024-12-31 00:30:06 +0200
commit77f6f957480d3674756ab6e5166641a3bab3e696 (patch)
tree27b9cffcc646b44a71828a6ed95ddc811f125f8f /htmtl/expression_modifiers/truncate.py
parentcc92131a707f05e54a8a74ca8a61a6953c1d3244 (diff)
bump
Diffstat (limited to 'htmtl/expression_modifiers/truncate.py')
-rw-r--r--htmtl/expression_modifiers/truncate.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/htmtl/expression_modifiers/truncate.py b/htmtl/expression_modifiers/truncate.py
new file mode 100644
index 0000000..41a1bdb
--- /dev/null
+++ b/htmtl/expression_modifiers/truncate.py
@@ -0,0 +1,16 @@
+from typing import Any
+
+from ..expression_modifier import ExpressionModifier, modifier
+
+
+@modifier("truncate")
+class Truncate(ExpressionModifier):
+ def modify(self, value: Any, opts: list[Any]) -> Any:
+ if isinstance(value, str) and len(opts) > 0:
+ if all([x in "1234567890" for x in opts[0]]):
+ char_limit = int(opts[0])
+
+ if len(value) > char_limit:
+ return f"{value[:char_limit - 3]}..."
+
+ return value