Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
htmlescape.py
#1
just finished up my version of html_escape():

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import division, print_function
"""Convert text string with HTML to HTML that shows it as text.

file          htmlescape.py
purpose       convert text string with HTML to HTML that shows it as text
author        Phil D. Howard
email         10054452614123394844460370234029112340408691

The intent is that this command works correctly under both Python 2 and
Python 3.  Please report failures or code improvement to the author.
"""

__license__ = """
Copyright © 2017, by Phil D. Howard - all other rights reserved

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

The author may be contacted by decoding the number
10054452614123394844460370234029112340408691
(provu igi la numeron al duuma)
"""

from sys import argv, stderr, stdin, stdout, version, version_info


# map is indexed by a character to get the escape string
# map is a list, not a dictionary
map = [chr(n) for n in range(256)]
for x in range(32):
   map[x] = '\\x' + hex(x).replace('x','')[-2:]
for x in range(128,256):
   map[x] = '\\x' + hex(x).replace('x','')[-2:]
for x in range(7):
   map[x+7] = '\\' + 'abtnvfr'[x]
del x
map[ord('&')] = '&'
map[ord('<')] = '&lt;'
map[ord('>')] = '&gt;'
map[ord('"')] = '&quot;'
map[39] = ''' # '
map[92] = '\' # \\
# these are shorter than their &#nnn; numeric escape
map[165] = '&yen;'
map[168] = '&uml;'
map[172] = '&not;'
map[173] = '&shy;'
map[174] = '&reg;'
map[176] = '&deg;'
map[208] = '&ETH;'
map[240] = '&eth;'


def html_escape(s):
   """Convert text string with HTML to HTML that shows it as text."""
   return ''.join([map[ord(c)] for c in s])


def main(args):
   """main"""
   if len(args) > 1:
       vparts = version.split('\n')
       print('Python',vparts[0].strip(),file=stderr)
       print('compiled by',vparts[1],file=stderr)
       return args[0] + ' is a module, not a command'
   else:
       for input in stdin:
           print(html_escape(input.strip())+'<br>')
           stdout.flush()
       return 0

if __name__ == '__main__':
   try:
       result=main(argv)
       stdout.flush()
   except BrokenPipeError:
       result=99
   except KeyboardInterrupt:
       print('')
       result=98
   if result is 0 or result is None or result is True:
       exit(0)
   if result is 1 or result is False:
       exit(1)
   if isinstance(result,str):
       print(result,file=stderr)
       exit(2)
   try:
       exit(int(result))
   except ValueError:
       print(str(result),file=stderr)
       exit(3)
   except TypeError:
       exit(4)
# EOF
Skaaaaaperen........
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)