about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2022-08-04 17:01:43 +0900
committerNguyễn Gia Phong <mcsinyx@disroot.org>2022-08-04 17:26:48 +0900
commit978611599094c21d4b2865f417479795608c4a83 (patch)
treed14a7cd55011e85fa944c9bc0ca92522ab194629 /src
parent9162549a27b02f5f9ce9adea3b957618db460779 (diff)
downloadrsskey-978611599094c21d4b2865f417479795608c4a83.tar.gz
Truncate long titles HEAD 0.2.0 main
Introduce option 'cw' for this and rename 'limit' to 'text' for clarity.
Diffstat (limited to 'src')
-rwxr-xr-xsrc/rsskey.py11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/rsskey.py b/src/rsskey.py
index c8acf53..91bf789 100755
--- a/src/rsskey.py
+++ b/src/rsskey.py
@@ -27,6 +27,12 @@ from markdownify import markdownify as md
 from trio import open_nursery, run
 
 
+def truncate(string, length, end='…'):
+    """Return string truncated to length, ending with given character."""
+    if len(string) <= length: return string
+    return string[:length-len(end)] + end
+
+
 async def create(client, **note):
     """Create the given note and return its ID."""
     response = await client.post('notes/create', json=note)
@@ -42,12 +48,13 @@ async def post(job, client, link, title, summary):
                                                      'userId': job['user']})
     if search.json(): return
 
-    note = partial(create, client, i=job['token'], visibility='home', cw=title)
+    note = partial(create, client, i=job['token'], visibility='home',
+                   cw=truncate(title, job.getint('cw')))
     original = f'Original: {link}'
     rest = '\n\n'.join((*map(partial(sub, r'\s+', ' '),
                              split(r'\s*\n{2,}\s*', md(summary).strip())),
                         original))
-    limit = int(job['limit'])
+    limit = job.getint('text')
     parent = None
 
     while len(rest) > limit: