Package: excerpt-html
Excerpt HTMLThis distribution provides a single function,excerpt_htmlwhose
purpose is to extract leading portions of HTML text.  This is useful,
for example, in order to be able to generate a summary of a blog post
from the post body.excerpt_html(html_text, min_words=50, cut_mark=r'(?i)\s*more\b')Theexcerpt_htmlfunction expects, as input, HTML text, and returns
a shortened version of that HTML text.  The truncation point is found
in one of two ways:If an explicitcut-mark— an HTML comment whose text matchescut_mark— is found, the text will be truncated there.If no explicit cut-mark is found, an attempt will be made to find a
suitable implicit truncation point.  Only points which
are not within in-line markup are considered.  The text will be
truncated at the first such location found which preserves at
leastmin_words(by default, 50) words of text.In either case, the returned excerpt will always be a syntactically
valid HTML fragment.Arguments:html_text: The input text, a string containing an HTML fragment.min_words: When finding a block-level truncation point, retain at
least this many words of the original text.  PassNoneto
disable block-level truncation.cut_mark: A regular expression which is to be matched against the
text of HTML comments inhtml_textto find a truncation point.
This is matched usingre.match()against the contents of HTML
comments.  This should be either a compiled regular expression or a
string; orNoneto disable cut-mark recognition.Returns:If a truncation point was found, a string containing the excerpt, a semantically valid HTML fragment, is returned.If no suitable truncation point was found,Noneis returned.InstallationThe package is installable viapip.pipinstallexcerpt-htmlExampleHere are two paragraphs worth of HTML, with an explicitcut-markin the middle
of the first paragraph.>>>fromexcerpt_htmlimportexcerpt_html>>>post_body='''...<p>...In a sense, the subject is interpolated into a neotextual...narrative that includes culture as a paradox....<!-- more -->...A number of deconceptualisms concerning substructural...construction exist....</p>...<p>...However, the subject is contextualised into a postmaterial...discourse that includes sexuality as a totality. Sontag uses...the term ‘cultural narrative’ to denote not, in fact,...deconstruction, but predeconstruction....</p>'''By default, the text will be truncated at the cut mark:>>>summary=excerpt_html(post_body)>>>print(summary)<p>In a sense, the subject is interpolated into a neotextualnarrative that includes culture as a paradox.</p>If we disablecut_markrecognition, there is no suitable implicit
truncation point which will preserve at least 50 words (the default
value ofmin_words):>>>summary=excerpt_html(post_body,cut_mark=None)>>>summaryisNoneTrueIf we a lower value formin_words, the break between paragraphs will be
selected as a truncation point:>>>summary=excerpt_html(post_body,min_words=10,cut_mark=None)>>>print(summary)# doctest: +NORMALIZE_WHITESPACE<p>In a sense, the subject is interpolated into a neotextualnarrative that includes culture as a paradox.<!-- more -->A number of deconceptualisms concerning substructuralconstruction exist.</p>LinksDevelopment takes place atGitHub.  Releases may be downloaded fromPyPI.AuthorJeff Dairikidairiki@dairiki.orgChangelogRelease 0.2 (2022-09-28)Fix deprecation warnings frombeautifulsoup4>=4.11.0.Test under python 3.10Drop support for python 3.6Release 0.1 (2021-02-05)No code changes.Update development status classifier to "stable".Release 0.1b2 (brown bag) (2020-05-09)Remove spurious lektor entry point declaration fromsetup.cfg.Release 0.1b1 (2020-05-09)Initial release.