moosey-cms 0.5.0__py3-none-any.whl → 0.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
moosey_cms/filters.py
CHANGED
|
@@ -47,13 +47,15 @@ def iso_date(dt):
|
|
|
47
47
|
return dt.strftime('%Y-%m-%d')
|
|
48
48
|
|
|
49
49
|
|
|
50
|
-
def relative_time(dt):
|
|
50
|
+
def relative_time(dt, showAgo=True):
|
|
51
51
|
"""Format date as relative time (e.g., '2 hours ago', 'yesterday')"""
|
|
52
52
|
if not dt:
|
|
53
53
|
return ""
|
|
54
54
|
|
|
55
55
|
now = datetime.now()
|
|
56
56
|
diff = now - dt
|
|
57
|
+
|
|
58
|
+
ago = " ago" if showAgo else ""
|
|
57
59
|
|
|
58
60
|
seconds = diff.total_seconds()
|
|
59
61
|
|
|
@@ -61,10 +63,10 @@ def relative_time(dt):
|
|
|
61
63
|
return "just now"
|
|
62
64
|
elif seconds < 3600:
|
|
63
65
|
minutes = int(seconds / 60)
|
|
64
|
-
return f"{minutes} minute{'s' if minutes != 1 else ''}
|
|
66
|
+
return f"{minutes} minute{'s' if minutes != 1 else ''}{ago}"
|
|
65
67
|
elif seconds < 86400:
|
|
66
68
|
hours = int(seconds / 3600)
|
|
67
|
-
return f"{hours} hour{'s' if hours != 1 else ''}
|
|
69
|
+
return f"{hours} hour{'s' if hours != 1 else ''}{ago}"
|
|
68
70
|
elif seconds < 172800:
|
|
69
71
|
return "yesterday"
|
|
70
72
|
elif seconds < 604800:
|
|
@@ -72,13 +74,13 @@ def relative_time(dt):
|
|
|
72
74
|
return f"{days} days ago"
|
|
73
75
|
elif seconds < 2592000:
|
|
74
76
|
weeks = int(seconds / 604800)
|
|
75
|
-
return f"{weeks} week{'s' if weeks != 1 else ''}
|
|
77
|
+
return f"{weeks} week{'s' if weeks != 1 else ''}{ago}"
|
|
76
78
|
elif seconds < 31536000:
|
|
77
79
|
months = int(seconds / 2592000)
|
|
78
|
-
return f"{months} month{'s' if months != 1 else ''}
|
|
80
|
+
return f"{months} month{'s' if months != 1 else ''}{ago}"
|
|
79
81
|
else:
|
|
80
82
|
years = int(seconds / 31536000)
|
|
81
|
-
return f"{years} year{'s' if years != 1 else ''}
|
|
83
|
+
return f"{years} year{'s' if years != 1 else ''}{ago}"
|
|
82
84
|
|
|
83
85
|
|
|
84
86
|
def time_only(dt):
|
|
@@ -90,7 +92,8 @@ def time_only(dt):
|
|
|
90
92
|
formatted = formatted[1:]
|
|
91
93
|
return formatted
|
|
92
94
|
|
|
93
|
-
|
|
95
|
+
def strptime(s, fmt):
|
|
96
|
+
return datetime.strptime(s, fmt)
|
|
94
97
|
# ============================================================================
|
|
95
98
|
# CURRENCY FILTERS
|
|
96
99
|
# ============================================================================
|
|
@@ -472,6 +475,43 @@ def read_time(text: str) -> str:
|
|
|
472
475
|
return "1 min read"
|
|
473
476
|
return f"{minutes} min read"
|
|
474
477
|
|
|
478
|
+
|
|
479
|
+
# ============================================================================
|
|
480
|
+
# HTML UTILITIES
|
|
481
|
+
# ============================================================================
|
|
482
|
+
|
|
483
|
+
def strip_comments(text, enabled=True):
|
|
484
|
+
"""
|
|
485
|
+
Removes HTML comments from the output.
|
|
486
|
+
Usage: {% filter strip_comments(enabled=True) %} ... {% endfilter %}
|
|
487
|
+
"""
|
|
488
|
+
if not enabled or not text:
|
|
489
|
+
return text
|
|
490
|
+
|
|
491
|
+
# Regex: Matches <!-- followed by anything (including newlines) until -->
|
|
492
|
+
# The *? ensures it is non-greedy (stops at the first closing tag)
|
|
493
|
+
return re.sub(r'<!--[\s\S]*?-->', '', str(text))
|
|
494
|
+
|
|
495
|
+
def minify_html(text, enabled=True):
|
|
496
|
+
"""
|
|
497
|
+
Minifies HTML by removing unnecessary whitespace and newlines.
|
|
498
|
+
WARNING: This is a regex-based minifier. It does not respect <pre> tags.
|
|
499
|
+
"""
|
|
500
|
+
if not enabled or not text:
|
|
501
|
+
return text
|
|
502
|
+
|
|
503
|
+
text = str(text)
|
|
504
|
+
|
|
505
|
+
# 1. Normalize whitespace:
|
|
506
|
+
# Replace sequences of whitespace (tabs, newlines) with a single space
|
|
507
|
+
text = re.sub(r'\s+', ' ', text)
|
|
508
|
+
|
|
509
|
+
# 2. Remove space between tags:
|
|
510
|
+
# Turns "</div> <div..." into "</div><div..."
|
|
511
|
+
text = re.sub(r'>\s+<', '><', text)
|
|
512
|
+
|
|
513
|
+
return text.strip()
|
|
514
|
+
|
|
475
515
|
# ============================================================================
|
|
476
516
|
# REGISTRATION FUNCTION
|
|
477
517
|
# ============================================================================
|
|
@@ -492,6 +532,7 @@ def register_filters(jinja_env):
|
|
|
492
532
|
'short_date': short_date,
|
|
493
533
|
'iso_date': iso_date,
|
|
494
534
|
'relative_time': relative_time,
|
|
535
|
+
'strptime': strptime,
|
|
495
536
|
'time_only': time_only,
|
|
496
537
|
'currency': currency,
|
|
497
538
|
'compact_currency': compact_currency,
|
|
@@ -511,7 +552,10 @@ def register_filters(jinja_env):
|
|
|
511
552
|
'filesize': filesize,
|
|
512
553
|
'default_if_none': default_if_none,
|
|
513
554
|
'yesno': yesno,
|
|
514
|
-
'read_time':read_time
|
|
555
|
+
'read_time':read_time,
|
|
556
|
+
'strip_comments': strip_comments,
|
|
557
|
+
'minify_html': minify_html,
|
|
558
|
+
|
|
515
559
|
}
|
|
516
560
|
|
|
517
561
|
for name, func in filters_dict.items():
|
moosey_cms/main.py
CHANGED
|
@@ -24,6 +24,30 @@ from .hot_reload_script import inject_script_middleware
|
|
|
24
24
|
|
|
25
25
|
from fastapi import WebSocket, WebSocketDisconnect
|
|
26
26
|
|
|
27
|
+
from jinja2 import Environment, FileSystemLoader
|
|
28
|
+
from jinja2.ext import Extension
|
|
29
|
+
import re
|
|
30
|
+
|
|
31
|
+
class AutoRemoveCommentsExtension(Extension):
|
|
32
|
+
"""Automatically removes HTML comments from all included files"""
|
|
33
|
+
|
|
34
|
+
def __init__(self, environment):
|
|
35
|
+
super().__init__(environment)
|
|
36
|
+
|
|
37
|
+
# Store original include function
|
|
38
|
+
original_include = environment.globals['include']
|
|
39
|
+
|
|
40
|
+
# Create wrapper that removes comments
|
|
41
|
+
def include_no_comments(template_name, **kwargs):
|
|
42
|
+
# Get the included template
|
|
43
|
+
included = environment.get_template(template_name)
|
|
44
|
+
rendered = included.render(**kwargs)
|
|
45
|
+
# Remove comments
|
|
46
|
+
return re.sub(r'<!--.*?-->', '', rendered, flags=re.DOTALL)
|
|
47
|
+
|
|
48
|
+
# Replace include function
|
|
49
|
+
environment.globals['include_no_comments'] = include_no_comments
|
|
50
|
+
|
|
27
51
|
|
|
28
52
|
class ConnectionManager:
|
|
29
53
|
def __init__(self):
|
|
@@ -81,6 +105,7 @@ def init_cms(
|
|
|
81
105
|
# This ensures site_data is available in 404.html and base.html automatically
|
|
82
106
|
templates.env.globals["site_data"] = site_data
|
|
83
107
|
templates.env.globals["mode"] = mode
|
|
108
|
+
|
|
84
109
|
|
|
85
110
|
# Register all custom filters once
|
|
86
111
|
filters.register_filters(templates.env)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: moosey-cms
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.0
|
|
4
4
|
Summary: Add your description here
|
|
5
5
|
Requires-Python: >=3.9
|
|
6
6
|
Requires-Dist: cachetools>=6.2.4
|
|
@@ -259,6 +259,8 @@ Moosey CMS comes packed with a comprehensive library of Jinja2 filters to help y
|
|
|
259
259
|
|
|
260
260
|
---
|
|
261
261
|
|
|
262
|
+
[Read More On Filters](docs/filters.md) and how to use some interesting ones such as stripping comments.
|
|
263
|
+
|
|
262
264
|
## ⚙️ Configuration Reference
|
|
263
265
|
|
|
264
266
|
The `init_cms` function accepts the following parameters:
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
moosey_cms/__init__.py,sha256=y7gzxC1LB7qRmjqJHJpN4kEqBNAbuIwNc4xeEI2clMY,184
|
|
2
2
|
moosey_cms/cache.py,sha256=YI6rRb4OVi-Mb1CmMW-jRz0CC9U6YZyszqLmjqLOsq8,2067
|
|
3
3
|
moosey_cms/file_watcher.py,sha256=0miTFpKZuT8aZPTByC0OVRr8A0mIG-fgPGrz2QfMG1k,915
|
|
4
|
-
moosey_cms/filters.py,sha256=
|
|
4
|
+
moosey_cms/filters.py,sha256=BjU30NAqV5viptVHRT__sSBLTxlQ-zObo571kc-cTfE,17410
|
|
5
5
|
moosey_cms/helpers.py,sha256=m94mDAaDKiH6wvzqoICQuavJ53EVTJ5VmJiRsfLT38o,10277
|
|
6
6
|
moosey_cms/hot_reload_script.py,sha256=394R-AtjCWEMjLp1ONdvzfg6ETp8TXMF8psoklW_z5c,3074
|
|
7
|
-
moosey_cms/main.py,sha256=
|
|
7
|
+
moosey_cms/main.py,sha256=oj_tePjiZXeOT4ujv8dMqodgZ7n5OCUYr5wROFboYwg,9815
|
|
8
8
|
moosey_cms/md.py,sha256=m857SKApJkK62wNrMVsypuJAqumbBt5GuPvcnuN1O6w,4970
|
|
9
9
|
moosey_cms/models.py,sha256=kYNIf7utTq94PUdqqZXfil5vpp0wciI5UgqRHJ56A0E,3070
|
|
10
10
|
moosey_cms/seo.py,sha256=jQ2FVuELNoytJkbp0ILK_IF7sZSaz9fkl59HM7xlg70,5246
|
|
11
11
|
moosey_cms/static/js/reload-script.js,sha256=hnrVXEWeTK-Y2vLeADmtlZ7fOXpDJMF-0zK09o3mrOA,2247
|
|
12
|
-
moosey_cms-0.
|
|
13
|
-
moosey_cms-0.
|
|
14
|
-
moosey_cms-0.
|
|
12
|
+
moosey_cms-0.7.0.dist-info/METADATA,sha256=rq8Yc8AzMBXrJq11PhxT9vEXOTI4WPBLOTCbLYhtm7s,11005
|
|
13
|
+
moosey_cms-0.7.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
14
|
+
moosey_cms-0.7.0.dist-info/RECORD,,
|
|
File without changes
|