Title is taken from `article.title`
-Body is taken from `article.summary` with standard Pelican configuartion i.e. length trimmed to 50 words and summary ends with `...`
+Body is taken from `article.summary` with standard Pelican configuartion i.e. length trimmed to 50 words and summary ends with `...` OR with parameters you set in `pelicanconf.py` (`SUMMARY_MAX_LENGTH` and `SUMMARY_END_SUFFIX`).
-In a future release *Pelican-toot* must have its own configuration parameter for length of text since Mastodon accepts posts with max 500 bytes.
+If the total length of the post exceeds MAX length allowed from Mastodon, then *Pelican-toot* will trim `article.summary` accordingly.
Hashtag(s) are taken - if any - from `article.tags` and concatenated separating each of them with commas.
Pelican can handle tags with whitespaces (for example `#My nice article`) without problems but hashtags in Mastodon are all written without. For this reason all whitespaces from Pelican hashtags will be removed before publishing (`#Mynicearticle`).
-Now if your post exceeds this limit you simply will receive the errore message `your toot exceeds Mastodon max limit...`
-
## Mastodon APIs
This plugin depends on [Mastodon.py](https://github.com/halcy/Mastodon.py).
api_base_url = mt_base_url
)
# Actually build the post structure
+ # First set a maximum length for the final post
+ toot_maxlength = 490 # Actually 500 but let's keep a safety gap for miscalculation...
for article in new_posts:
articleslist.append(article.url)
titlehtmltext = article.title
articlehtmltext = article.summary
articlecleantext = html.fromstring(articlehtmltext)
summary_to_publish = articlecleantext.text_content().strip() + '\n'
- read_more = 'Read more: ' + article.get_siteurl() + '/' + article.url + '\n\n'
+ read_more = 'Leggi tutto: ' + article.get_siteurl() + '/' + article.url + '\n\n'
if hasattr(article, 'tags'):
taglist = article.tags
new_taglist = []
for i in taglist:
new_taglist.append('#' + str(i))
tags_to_publish = ', '.join(str(x).replace(" ", "") for x in new_taglist)
- mastodon_toot = title_to_publish + summary_to_publish + read_more + tags_to_publish
+ toot_length = len(title_to_publish) + len(summary_to_publish) + len(read_more) + len(tags_to_publish)
+ if toot_length > toot_maxlength:
+ truncate = toot_maxlength - len(title_to_publish) - len(tags_to_publish) - len(read_more) - 4
+ summary_to_publish = summary_to_publish[:truncate] + ' ...' + '\n'
+ mastodon_toot = title_to_publish + summary_to_publish + read_more + tags_to_publish
+ else:
+ mastodon_toot = title_to_publish + summary_to_publish + read_more + tags_to_publish
else:
- mastodon_toot = title_to_publish + summary_to_publish + read_more
- # check the length of the final post
- toot_maxlength = 490 # Actually 500 but let's keep a safety gap...
- if len(mastodon_toot) >= toot_maxlength:
- logger.warning('Pelican_toot: your toot exceeds Mastodon max limit... ')
- sys.exit(9)
- else:
- mastodon.toot(mastodon_toot)
+ toot_length = len(title_to_publish) + len(summary_to_publish) + len(read_more)
+ if toot_length > toot_maxlength:
+ truncate = toot_maxlength - len(title_to_publish) - len(read_more) - 4
+ summary_to_publish = summary_to_publish[:truncate] + ' ...' + '\n'
+ mastodon_toot = title_to_publish + summary_to_publish + read_more
+ else:
+ mastodon_toot = title_to_publish + summary_to_publish + read_more
+ mastodon.toot(mastodon_toot)
write_articleslist(articleslist)
def register():
try:
signals.article_writer_finalized.connect(post_updates)
except AttributeError:
- pass
+ pass
\ No newline at end of file