Example #1
0
 def test_fixture(url, expected_html):
     print('url=%s' % url)
     
     actual_html = readable(url)
     
     # Write the HTML for later diagnosis
     _, actual_html_fn = tempfile.mkstemp('readabilitytest')
     os.close(_)
     _, expected_html_fn = tempfile.mkstemp('readabilitytest')
     os.close(_)
     with codecs.open(actual_html_fn, 'w', DEFAULT_ENCODING) as f:
         f.write(actual_html)
     with codecs.open(expected_html_fn, 'w', DEFAULT_ENCODING) as f:
         f.write(expected_html)
     
     # Verify that there is no 'diff' between the two versions
     diff = list(difflib.context_diff(
         actual_html.splitlines(), expected_html.splitlines()))
     diff_string = '\n'.join(diff)
     assert not diff, (
         'readable version differs; diff:\n{0}\n'
         'expected: {1}\n'
         'actual: {2}').format(
         diff_string,
         expected_html_fn,
         actual_html_fn,
     )
Example #2
0
    def test_fixture(url, original_html, expected_html):
        print('url=%s' % url)

        actual_html = readable(url, original_html)

        # Write the HTML for later diagnosis
        _, actual_html_fn = tempfile.mkstemp('readabilitytest')
        os.close(_)
        _, expected_html_fn = tempfile.mkstemp('readabilitytest')
        os.close(_)
        with codecs.open(actual_html_fn, 'w', DEFAULT_ENCODING) as f:
            f.write(actual_html)
        with codecs.open(expected_html_fn, 'w', DEFAULT_ENCODING) as f:
            f.write(expected_html)

        # Verify that there is no 'diff' between the two versions
        diff = list(
            difflib.context_diff(actual_html.splitlines(),
                                 expected_html.splitlines()))
        diff_string = '\n'.join(diff)
        assert not diff, ('readable version differs; diff:\n{0}\n'
                          'expected: {1}\n'
                          'actual: {2}').format(
                              diff_string,
                              expected_html_fn,
                              actual_html_fn,
                          )
Example #3
0
def main():
    import webbrowser
    from tempfile import mkstemp
    from optparse import OptionParser
    import codecs
    
    usage = "usage: %prog [options] URL1 URL2 ..."
    parser = OptionParser(usage=usage)
    parser.add_option(b"-b", b"--open-browser",
                  action="store_true", dest="open_browser", default=False,
                  help=b"show the readable version in a web browser")
    (options, args) = parser.parse_args()
    
    if not args:
        print(parser.format_help())
        sys.exit(2)
    
    for url in args:
        html = urllib.urlopen(url).read().decode(DEFAULT_ENCODING)
        readable_html = readable(url, html)
        if options.open_browser:
            fd, fn = mkstemp('readability.html')
            os.close(fd)
            with codecs.open(fn, 'w', encoding=DEFAULT_ENCODING) as f:
                f.write(readable_html)
            webbrowser.open('file://' + os.path.abspath(fn))
        else:
            print(readable_html)
Example #4
0
def main():
    import webbrowser
    from tempfile import mkstemp
    from optparse import OptionParser
    import codecs

    usage = "usage: %prog [options] URL1 URL2 ..."
    parser = OptionParser(usage=usage)
    parser.add_option(b"-b",
                      b"--open-browser",
                      action="store_true",
                      dest="open_browser",
                      default=False,
                      help=b"show the readable version in a web browser")
    (options, args) = parser.parse_args()

    if not args:
        print(parser.format_help())
        sys.exit(2)

    for url in args:
        html = urllib.urlopen(url).read().decode(DEFAULT_ENCODING)
        readable_html = readable(url, html)
        if options.open_browser:
            fd, fn = mkstemp('readability.html')
            os.close(fd)
            with codecs.open(fn, 'w', encoding=DEFAULT_ENCODING) as f:
                f.write(readable_html)
            webbrowser.open('file://' + os.path.abspath(fn))
        else:
            print(readable_html)