mirror of
https://github.com/viq/NewsBlur.git
synced 2025-09-18 21:43:31 +00:00
De-vendorizing pynliner.
This commit is contained in:
parent
6e93a92f64
commit
f281b7cd5e
4 changed files with 2 additions and 377 deletions
|
@ -12,6 +12,7 @@ import random
|
|||
import requests
|
||||
import html.parser as html_parser
|
||||
import tweepy
|
||||
import pynliner
|
||||
from collections import defaultdict
|
||||
from bs4 import BeautifulSoup
|
||||
from mongoengine.queryset import Q
|
||||
|
@ -30,7 +31,6 @@ from apps.rss_feeds.text_importer import TextImporter
|
|||
from apps.rss_feeds.page_importer import PageImporter
|
||||
from apps.profile.models import Profile, MSentEmail
|
||||
from vendor import facebook
|
||||
from vendor import pynliner
|
||||
from utils import log as logging
|
||||
from utils import json_functions as json
|
||||
from utils.feed_functions import relative_timesince, chunks
|
||||
|
|
|
@ -52,6 +52,7 @@ pytest==6.1.2
|
|||
python-dateutil==2.8.1
|
||||
python-digitalocean==1.16.0
|
||||
python-gflags==3.1.2
|
||||
pynliner==0.8
|
||||
pytz==2020.4
|
||||
pyyaml==5.3.1
|
||||
raven==6.10.0
|
||||
|
|
247
vendor/pynliner/__init__.py
vendored
247
vendor/pynliner/__init__.py
vendored
|
@ -1,247 +0,0 @@
|
|||
#!/usr/bin/env python
|
||||
"""Pynliner : Convert CSS to inline styles
|
||||
|
||||
Python CSS-to-inline-styles conversion tool for HTML using BeautifulSoup and cssutils
|
||||
|
||||
Copyright (c) 2011 Tanner Netterville
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
The generated output of this software shall not be used in a mass marketing service.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
"""
|
||||
|
||||
__version__ = "0.4.0"
|
||||
|
||||
import urllib.request
|
||||
import cssutils
|
||||
from bs4 import BeautifulSoup
|
||||
from .soupselect import select
|
||||
|
||||
class Pynliner(object):
|
||||
"""Pynliner class"""
|
||||
|
||||
soup = False
|
||||
style_string = False
|
||||
stylesheet = False
|
||||
output = False
|
||||
|
||||
def __init__(self, log=None):
|
||||
self.log = log
|
||||
cssutils.log.enabled = False if log is None else True
|
||||
|
||||
def from_url(self, url):
|
||||
"""Gets remote HTML page for conversion
|
||||
|
||||
Downloads HTML page from `url` as a string and passes it to the
|
||||
`from_string` method. Also sets `self.root_url` and `self.relative_url`
|
||||
for use in importing <link> elements.
|
||||
|
||||
Returns self.
|
||||
|
||||
>>> p = Pynliner()
|
||||
>>> p.from_url('http://somewebsite.com/file.html')
|
||||
<Pynliner object at 0x26ac70>
|
||||
"""
|
||||
self.url = url
|
||||
self.relative_url = '/'.join(url.split('/')[:-1]) + '/'
|
||||
self.root_url = '/'.join(url.split('/')[:3])
|
||||
self.source_string = self._get_url(self.url)
|
||||
return self
|
||||
|
||||
def from_string(self, string):
|
||||
"""Generates a Pynliner object from the given HTML string.
|
||||
|
||||
Returns self.
|
||||
|
||||
>>> p = Pynliner()
|
||||
>>> p.from_string('<style>h1 { color:#ffcc00; }</style><h1>Hello World!</h1>')
|
||||
<Pynliner object at 0x26ac70>
|
||||
"""
|
||||
self.source_string = string
|
||||
return self
|
||||
|
||||
def with_cssString(self, cssString):
|
||||
"""Adds external CSS to the Pynliner object. Can be "chained".
|
||||
|
||||
Returns self.
|
||||
|
||||
>>> html = "<h1>Hello World!</h1>"
|
||||
>>> css = "h1 { color:#ffcc00; }"
|
||||
>>> p = Pynliner()
|
||||
>>> p.from_string(html).with_cssString(css)
|
||||
<pynliner.Pynliner object at 0x2ca810>
|
||||
"""
|
||||
if not self.style_string:
|
||||
self.style_string = cssString + '\n'
|
||||
else:
|
||||
self.style_string += cssString + '\n'
|
||||
return self
|
||||
|
||||
def run(self):
|
||||
"""Applies each step of the process if they have not already been
|
||||
performed.
|
||||
|
||||
Returns Unicode output with applied styles.
|
||||
|
||||
>>> html = "<style>h1 { color:#ffcc00; }</style><h1>Hello World!</h1>"
|
||||
>>> Pynliner().from_string(html).run()
|
||||
u'<h1 style="color: #fc0">Hello World!</h1>'
|
||||
"""
|
||||
if not self.soup:
|
||||
self._get_soup()
|
||||
if not self.stylesheet:
|
||||
self._get_styles()
|
||||
self._apply_styles()
|
||||
return self._get_output()
|
||||
|
||||
def _get_url(self, url):
|
||||
"""Returns the response content from the given url
|
||||
"""
|
||||
return urllib.request.urlopen(url).read()
|
||||
|
||||
def _get_soup(self):
|
||||
"""Convert source string to BeautifulSoup object. Sets it to self.soup.
|
||||
|
||||
If using mod_wgsi, use html5 parsing to prevent BeautifulSoup incompatibility.
|
||||
"""
|
||||
# Check if mod_wsgi is running - see http://code.google.com/p/modwsgi/wiki/TipsAndTricks
|
||||
try:
|
||||
from mod_wsgi import version
|
||||
self.soup = BeautifulSoup(self.source_string, "html5lib")
|
||||
except:
|
||||
self.soup = BeautifulSoup(self.source_string)
|
||||
|
||||
def _get_styles(self):
|
||||
"""Gets all CSS content from and removes all <link rel="stylesheet"> and
|
||||
<style> tags concatenating into one CSS string which is then parsed with
|
||||
cssutils and the resulting CSSStyleSheet object set to
|
||||
`self.stylesheet`.
|
||||
"""
|
||||
self._get_external_styles()
|
||||
self._get_internal_styles()
|
||||
|
||||
cssparser = cssutils.CSSParser(log=self.log)
|
||||
self.stylesheet = cssparser.parseString(self.style_string)
|
||||
|
||||
def _get_external_styles(self):
|
||||
"""Gets <link> element styles
|
||||
"""
|
||||
if not self.style_string:
|
||||
self.style_string = ''
|
||||
else:
|
||||
self.style_string += '\n'
|
||||
|
||||
link_tags = self.soup.findAll('link', {'rel': 'stylesheet'})
|
||||
for tag in link_tags:
|
||||
url = tag['href']
|
||||
if url.startswith('http://'):
|
||||
pass
|
||||
elif url.startswith('/'):
|
||||
url = self.root_url + url
|
||||
else:
|
||||
url = self.relative_url + url
|
||||
self.style_string += self._get_url(url)
|
||||
tag.extract()
|
||||
|
||||
def _get_internal_styles(self):
|
||||
"""Gets <style> element styles
|
||||
"""
|
||||
if not self.style_string:
|
||||
self.style_string = ''
|
||||
else:
|
||||
self.style_string += '\n'
|
||||
|
||||
style_tags = self.soup.findAll('style')
|
||||
for tag in style_tags:
|
||||
self.style_string += '\n'.join(tag.contents) + '\n'
|
||||
tag.extract()
|
||||
|
||||
def _get_specificity_from_list(self, lst):
|
||||
"""
|
||||
Takes an array of ints and returns an integer formed
|
||||
by adding all ints multiplied by the power of 10 of the current index
|
||||
|
||||
(1, 0, 0, 1) => (1 * 10**3) + (0 * 10**2) + (0 * 10**1) + (1 * 10**0) => 1001
|
||||
"""
|
||||
return int(''.join(map(str, lst)))
|
||||
|
||||
def _get_rule_specificity(self, rule):
|
||||
"""
|
||||
For a given CSSRule get its selector specificity in base 10
|
||||
"""
|
||||
return sum(map(self._get_specificity_from_list, (s.specificity for s in rule.selectorList)))
|
||||
|
||||
def _apply_styles(self):
|
||||
"""Steps through CSS rules and applies each to all the proper elements
|
||||
as @style attributes prepending any current @style attributes.
|
||||
"""
|
||||
rules = self.stylesheet.cssRules.rulesOfType(1)
|
||||
elem_prop_map = {}
|
||||
elem_style_map = {}
|
||||
|
||||
# build up a property list for every styled element
|
||||
for rule in rules:
|
||||
# select elements for every selector
|
||||
selectors = rule.selectorText.split(',')
|
||||
elements = []
|
||||
for selector in selectors:
|
||||
elements += select(self.soup, selector)
|
||||
# build prop_list for each selected element
|
||||
for elem in elements:
|
||||
if elem not in elem_prop_map:
|
||||
elem_prop_map[elem] = []
|
||||
elem_prop_map[elem].append({
|
||||
'specificity': self._get_rule_specificity(rule),
|
||||
'props': rule.style.getProperties(),
|
||||
})
|
||||
|
||||
# build up another property list using selector specificity
|
||||
for elem, props in list(elem_prop_map.items()):
|
||||
if elem not in elem_style_map:
|
||||
elem_style_map[elem] = cssutils.css.CSSStyleDeclaration()
|
||||
# ascending sort of prop_lists based on specificity
|
||||
props = sorted(props, key=lambda p: p['specificity'])
|
||||
# for each prop_list, apply to CSSStyleDeclaration
|
||||
for prop_list in [obj['props'] for obj in props]:
|
||||
for prop in prop_list:
|
||||
elem_style_map[elem][prop.name] = prop.value
|
||||
|
||||
|
||||
# apply rules to elements
|
||||
for elem, style_declaration in list(elem_style_map.items()):
|
||||
if 'style' in elem:
|
||||
elem['style'] = '%s; %s' % (style_declaration.cssText.replace('\n', ' '), elem['style'])
|
||||
else:
|
||||
elem['style'] = style_declaration.cssText.replace('\n', ' ')
|
||||
|
||||
def _get_output(self):
|
||||
"""Generate Unicode string of `self.soup` and set it to `self.output`
|
||||
|
||||
Returns self.output
|
||||
"""
|
||||
self.output = str(self.soup)
|
||||
return self.output
|
||||
|
||||
def fromURL(url, log=None):
|
||||
"""Shortcut Pynliner constructor. Equivelent to:
|
||||
|
||||
>>> Pynliner().from_url(someURL).run()
|
||||
|
||||
Returns processed HTML string.
|
||||
"""
|
||||
return Pynliner(log).from_url(url).run()
|
||||
|
||||
def fromString(string, log=None):
|
||||
"""Shortcut Pynliner constructor. Equivelent to:
|
||||
|
||||
>>> Pynliner().from_string(someString).run()
|
||||
|
||||
Returns processed HTML string.
|
||||
"""
|
||||
return Pynliner(log).from_string(string).run()
|
||||
|
129
vendor/pynliner/soupselect.py
vendored
129
vendor/pynliner/soupselect.py
vendored
|
@ -1,129 +0,0 @@
|
|||
"""
|
||||
# Included with pynliner since it isn't on PyPI #
|
||||
|
||||
soupselect.py
|
||||
|
||||
CSS selector support for BeautifulSoup.
|
||||
|
||||
soup = BeautifulSoup('<html>...')
|
||||
select(soup, 'div')
|
||||
- returns a list of div elements
|
||||
|
||||
select(soup, 'div#main ul a')
|
||||
- returns a list of links inside a ul inside div#main
|
||||
|
||||
patched to support multiple class selectors here http://code.google.com/p/soupselect/issues/detail?id=4#c0
|
||||
|
||||
"""
|
||||
|
||||
import re
|
||||
|
||||
tag_re = re.compile('^[a-z0-9]+$')
|
||||
|
||||
attribselect_re = re.compile(
|
||||
r'^(?P<tag>\w+)?\[(?P<attribute>\w+)(?P<operator>[=~\|\^\$\*]?)' +
|
||||
r'=?"?(?P<value>[^\]"]*)"?\]$'
|
||||
)
|
||||
|
||||
# /^(\w+)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/
|
||||
# \---/ \---/\-------------/ \-------/
|
||||
# | | | |
|
||||
# | | | The value
|
||||
# | | ~,|,^,$,* or =
|
||||
# | Attribute
|
||||
# Tag
|
||||
|
||||
def attribute_checker(operator, attribute, value=''):
|
||||
"""
|
||||
Takes an operator, attribute and optional value; returns a function that
|
||||
will return True for elements that match that combination.
|
||||
"""
|
||||
return {
|
||||
'=': lambda el: el.get(attribute) == value,
|
||||
# attribute includes value as one of a set of space separated tokens
|
||||
'~': lambda el: value in el.get(attribute, '').split(),
|
||||
# attribute starts with value
|
||||
'^': lambda el: el.get(attribute, '').startswith(value),
|
||||
# attribute ends with value
|
||||
'$': lambda el: el.get(attribute, '').endswith(value),
|
||||
# attribute contains value
|
||||
'*': lambda el: value in el.get(attribute, ''),
|
||||
# attribute is either exactly value or starts with value-
|
||||
'|': lambda el: el.get(attribute, '') == value \
|
||||
or el.get(attribute, '').startswith('%s-' % value),
|
||||
}.get(operator, lambda el: attribute in el)
|
||||
|
||||
|
||||
def select(soup, selector):
|
||||
"""
|
||||
soup should be a BeautifulSoup instance; selector is a CSS selector
|
||||
specifying the elements you want to retrieve.
|
||||
"""
|
||||
tokens = selector.split()
|
||||
current_context = [soup]
|
||||
for token in tokens:
|
||||
m = attribselect_re.match(token)
|
||||
if m:
|
||||
# Attribute selector
|
||||
tag, attribute, operator, value = m.groups()
|
||||
if not tag:
|
||||
tag = True
|
||||
checker = attribute_checker(operator, attribute, value)
|
||||
found = []
|
||||
for context in current_context:
|
||||
found.extend([el for el in context.findAll(tag) if checker(el)])
|
||||
current_context = found
|
||||
continue
|
||||
if '#' in token:
|
||||
# ID selector
|
||||
tag, id = token.split('#', 1)
|
||||
if not tag:
|
||||
tag = True
|
||||
el = current_context[0].find(tag, {'id': id})
|
||||
if not el:
|
||||
return [] # No match
|
||||
current_context = [el]
|
||||
continue
|
||||
if '.' in token:
|
||||
# Class selector
|
||||
tag, klass = token.split('.', 1)
|
||||
if not tag:
|
||||
tag = True
|
||||
found = []
|
||||
for context in current_context:
|
||||
found.extend(
|
||||
context.findAll(tag,
|
||||
{'class': lambda attr: attr and set(klass.split('.')).issubset(attr.split())}
|
||||
)
|
||||
)
|
||||
current_context = found
|
||||
continue
|
||||
if token == '*':
|
||||
# Star selector
|
||||
found = []
|
||||
for context in current_context:
|
||||
found.extend(context.findAll(True))
|
||||
current_context = found
|
||||
continue
|
||||
# Here we should just have a regular tag
|
||||
if not tag_re.match(token):
|
||||
return []
|
||||
found = []
|
||||
for context in current_context:
|
||||
found.extend(context.findAll(token))
|
||||
current_context = found
|
||||
return current_context
|
||||
|
||||
def monkeypatch(BeautifulSoupClass=None):
|
||||
"""
|
||||
If you don't explicitly state the class to patch, defaults to the most
|
||||
common import location for BeautifulSoup.
|
||||
"""
|
||||
if not BeautifulSoupClass:
|
||||
from BeautifulSoup import BeautifulSoup as BeautifulSoupClass
|
||||
BeautifulSoupClass.findSelect = select
|
||||
|
||||
def unmonkeypatch(BeautifulSoupClass=None):
|
||||
if not BeautifulSoupClass:
|
||||
from BeautifulSoup import BeautifulSoup as BeautifulSoupClass
|
||||
delattr(BeautifulSoupClass, 'findSelect')
|
Loading…
Add table
Reference in a new issue