Skip to main content

Convert HTML to ANS

Project description

https://img.shields.io/pypi/v/html2ans.svg https://img.shields.io/pypi/pyversions/html2ans.svg https://circleci.com/gh/washingtonpost/html2ans.svg?style=shield https://img.shields.io/pypi/l/html2ans.svg

This project provides a standardized method of parsing HTML elements into ANS elements. It is mainly used by Arc Publishing’s professional services team to migrate client data into the Arc platform, but can also be used for arbitrary conversion of HTML to JSON.

html2ans is hosted on pypi.

Please use the GitHub issue tracker to submit bugs or request features.

Full documentation can be found here.

Quickstart

Generating ANS from HTML

from html2ans.default import Html2Ans

parser = Html2Ans()
content_elements = parser.generate_ans(your_html_here)

Adding Parsers

Basic Addition

If you need to parse a certain tag in a customized way, you can write your own parser class and add it to the parsers Html2Ans will use like so:

from html2ans.default import Html2Ans

parser = Html2Ans()
parser.add_parser(YourCustomImageParser())
parser.generate_ans(your_html_here)

The types of items your parser can parse should be listed in its applicable_elements attribute.

The default parser class (DefaultHtmlAnsParser or Html2Ans) has parsers for text, links, images, various social media embeds, etc.

Prioritized Addition

The parsers that can be used for each element type (e.g. img, p) are held in a list. If you want your parser to have a higher priority than the default parsers, add it like so:

from html2ans.default import Html2Ans

parser = Html2Ans()
parser.insert_parser('img', YourCustomImageParser(), 0)
parser.generate_ans(your_html_here)

Creating Custom Parsers

Missing from the snippet above is a definition of YourCustomImageParser. Before talking about how to create such a parser, let’s examine why you might need to do so.

The default image parser html2ans.parsers.image.ImageParser applies to html img tags only. Imagine you need to parse html whose images come in div tags (labelled with the class fancy-figure) that also hold a caption (labelled with the class fancy-caption). Here is a possible implementation of a parser for such images (note: this returns basic image ANS, not a reference):

from html2ans.parsers.image import ImageParser
from html2ans.parsers.base import ParseResult

class YourCustomImageParser(ImageParser):
    applicable_elements = ['div', 'figure']
    applicable_classes = ['fancy-figure']

    def parse(self, element, *args, **kwargs):
        image_tag = element.find('img')
        caption_tag = element.find('p', {"class": "fancy-caption"})
        if image_tag:
            image = self.construct_output(image_tag)
            if caption_tag:
              image["caption"] = caption_tag.text
            return ParseResult(image, True)
        return ParseResult(None, True)

Custom Parsing Tips

ANS Versions

Some ANS types require a version. You can set a version in your main parser (Html2Ans) and then automatically include that version in any element parser’s output by setting the parser’s version_required attribute to True.

Note: this doesn’t mean valid, version-compatible ANS is automatically produced!

Keeping HTML in text Output

To adjust what HTML is/isn’t left inline when parsing text, adjust the INLINE_TAGS attribute on the text parser. Every parser inherits from html2ans.parsers.utils.AbstractParserUtilities which provides a list of default INLINE_TAGS which can be used to make sure text formatters (e.g. strong, em, etc.) are left in place when text is parsed.

Removing Unnecessary Tags

Sometimes it is helpful to remove unnecessary tags (e.g. <p></p>, <div><img src="..." /></div>). By default, Html2Ans considers p and div tags with no attributes other than id, class, or style to be unnecessary “wrappers”. When these are encountered, they are ignored and their children are parsed.

The benefit of this is that <p></p> is ignored and <div><img src="..." /></div> is parsed as an image.

The downside is that sometimes you don’t want your HTML removed! There are a few options in this case. You can configure what tags can be considered wrappers via the WRAPPER_TAGS attribute on Html2Ans. So if div tags should never be removed, simply remove div from this list. If a more complicated set of rules are necessary, override the is_wrapper method on Html2Ans.

If it’s easier to modify the HTML than to modify this library, you can also add an arbitrary attribute like so: <div no_parse_flag="true">...</div>. This div will not be considered a wrapper when it is encountered.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

html2ans-3.0.3.tar.gz (18.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

html2ans-3.0.3-py3.6.egg (44.6 kB view details)

Uploaded Egg

html2ans-3.0.3-py2.py3-none-any.whl (22.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file html2ans-3.0.3.tar.gz.

File metadata

  • Download URL: html2ans-3.0.3.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for html2ans-3.0.3.tar.gz
Algorithm Hash digest
SHA256 170fe66e747a89d7511a736503278f21ecbc4665014479a97183b5143cae83d1
MD5 9b6ae67b7dc3631b8e1e6b81189141c6
BLAKE2b-256 44cf69614cf7a428563d587e06714f4679783f4771db5803355a71984fe320a5

See more details on using hashes here.

File details

Details for the file html2ans-3.0.3-py3.6.egg.

File metadata

  • Download URL: html2ans-3.0.3-py3.6.egg
  • Upload date:
  • Size: 44.6 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for html2ans-3.0.3-py3.6.egg
Algorithm Hash digest
SHA256 b95ec377c89d25f2cf878c1b1a20dcd96b3ee4a350f0ec9c9094a0852d5fc20b
MD5 21b700f2a12ee5e44c3f17b9b9cc9f55
BLAKE2b-256 701ef8a6f744733cf2e2cf1091afd5901d4c19ce14b452f2c72771731cfe7abe

See more details on using hashes here.

File details

Details for the file html2ans-3.0.3-py2.py3-none-any.whl.

File metadata

  • Download URL: html2ans-3.0.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.8

File hashes

Hashes for html2ans-3.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 19adf925716f56f27b795f182c91ec997206e47090152eb1efe8ec207df501e3
MD5 105d7cb141998494587ea5ea9f1f055c
BLAKE2b-256 5eefb3df3188ae0af99cf9482e75e81811224c053951579f2ce430ac05fc0048

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page