Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

775 rader
21 KiB

  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. # IMPORTANT: only import safe functions as this module will be included in jinja environment
  5. import frappe
  6. import operator
  7. import re, urllib, datetime, math
  8. import babel.dates
  9. from dateutil import parser
  10. from num2words import num2words
  11. import HTMLParser
  12. from html2text import html2text
  13. DATE_FORMAT = "%Y-%m-%d"
  14. TIME_FORMAT = "%H:%M:%S.%f"
  15. DATETIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT
  16. # datetime functions
  17. def getdate(string_date=None):
  18. """
  19. Coverts string date (yyyy-mm-dd) to datetime.date object
  20. """
  21. if not string_date:
  22. return get_datetime().date()
  23. if isinstance(string_date, datetime.datetime):
  24. return string_date.date()
  25. elif isinstance(string_date, datetime.date):
  26. return string_date
  27. # dateutil parser does not agree with dates like 0000-00-00
  28. if not string_date or string_date=="0000-00-00":
  29. return None
  30. return parser.parse(string_date).date()
  31. def get_datetime(datetime_str=None):
  32. if not datetime_str:
  33. return now_datetime()
  34. if isinstance(datetime_str, (datetime.datetime, datetime.timedelta)):
  35. return datetime_str
  36. elif isinstance(datetime_str, (list, tuple)):
  37. return datetime.datetime(datetime_str)
  38. elif isinstance(datetime_str, datetime.date):
  39. return datetime.datetime.combine(datetime_str, datetime.time())
  40. # dateutil parser does not agree with dates like 0000-00-00
  41. if not datetime_str or (datetime_str or "").startswith("0000-00-00"):
  42. return None
  43. return parser.parse(datetime_str)
  44. def to_timedelta(time_str):
  45. if isinstance(time_str, basestring):
  46. t = parser.parse(time_str)
  47. return datetime.timedelta(hours=t.hour, minutes=t.minute, seconds=t.second, microseconds=t.microsecond)
  48. else:
  49. return time_str
  50. def add_to_date(date, years=0, months=0, days=0):
  51. """Adds `days` to the given date"""
  52. from dateutil.relativedelta import relativedelta
  53. as_string, as_datetime = False, False
  54. if date==None:
  55. date = now_datetime()
  56. if isinstance(date, basestring):
  57. as_string = True
  58. if " " in date:
  59. as_datetime = True
  60. date = parser.parse(date)
  61. date = date + relativedelta(years=years, months=months, days=days)
  62. if as_string:
  63. if as_datetime:
  64. return date.strftime(DATETIME_FORMAT)
  65. else:
  66. return date.strftime(DATE_FORMAT)
  67. else:
  68. return date
  69. def add_days(date, days):
  70. return add_to_date(date, days=days)
  71. def add_months(date, months):
  72. return add_to_date(date, months=months)
  73. def add_years(date, years):
  74. return add_to_date(date, years=years)
  75. def date_diff(string_ed_date, string_st_date):
  76. return (getdate(string_ed_date) - getdate(string_st_date)).days
  77. def time_diff(string_ed_date, string_st_date):
  78. return get_datetime(string_ed_date) - get_datetime(string_st_date)
  79. def time_diff_in_seconds(string_ed_date, string_st_date):
  80. return time_diff(string_ed_date, string_st_date).total_seconds()
  81. def time_diff_in_hours(string_ed_date, string_st_date):
  82. return round(float(time_diff(string_ed_date, string_st_date).total_seconds()) / 3600, 6)
  83. def now_datetime():
  84. dt = convert_utc_to_user_timezone(datetime.datetime.utcnow())
  85. return dt.replace(tzinfo=None)
  86. def _get_time_zone():
  87. return frappe.db.get_system_setting('time_zone') or 'Asia/Kolkata'
  88. def get_time_zone():
  89. if frappe.local.flags.in_test:
  90. return _get_time_zone()
  91. return frappe.cache().get_value("time_zone", _get_time_zone)
  92. def convert_utc_to_user_timezone(utc_timestamp):
  93. from pytz import timezone, UnknownTimeZoneError
  94. utcnow = timezone('UTC').localize(utc_timestamp)
  95. try:
  96. return utcnow.astimezone(timezone(get_time_zone()))
  97. except UnknownTimeZoneError:
  98. return utcnow
  99. def now():
  100. """return current datetime as yyyy-mm-dd hh:mm:ss"""
  101. if frappe.flags.current_date:
  102. return getdate(frappe.flags.current_date).strftime(DATE_FORMAT) + " " + \
  103. now_datetime().strftime(TIME_FORMAT)
  104. else:
  105. return now_datetime().strftime(DATETIME_FORMAT)
  106. def nowdate():
  107. """return current date as yyyy-mm-dd"""
  108. return now_datetime().strftime(DATE_FORMAT)
  109. def today():
  110. return nowdate()
  111. def nowtime():
  112. """return current time in hh:mm"""
  113. return now_datetime().strftime(TIME_FORMAT)
  114. def get_first_day(dt, d_years=0, d_months=0):
  115. """
  116. Returns the first day of the month for the date specified by date object
  117. Also adds `d_years` and `d_months` if specified
  118. """
  119. dt = getdate(dt)
  120. # d_years, d_months are "deltas" to apply to dt
  121. overflow_years, month = divmod(dt.month + d_months - 1, 12)
  122. year = dt.year + d_years + overflow_years
  123. return datetime.date(year, month + 1, 1)
  124. def get_last_day(dt):
  125. """
  126. Returns last day of the month using:
  127. `get_first_day(dt, 0, 1) + datetime.timedelta(-1)`
  128. """
  129. return get_first_day(dt, 0, 1) + datetime.timedelta(-1)
  130. def get_time(time_str):
  131. if isinstance(time_str, datetime.datetime):
  132. return time_str.time()
  133. elif isinstance(time_str, datetime.time):
  134. return time_str
  135. else:
  136. if isinstance(time_str, datetime.timedelta):
  137. time_str = str(time_str)
  138. return parser.parse(time_str).time()
  139. def get_datetime_str(datetime_obj):
  140. if isinstance(datetime_obj, basestring):
  141. datetime_obj = get_datetime(datetime_obj)
  142. return datetime_obj.strftime(DATETIME_FORMAT)
  143. def get_user_format():
  144. if getattr(frappe.local, "user_format", None) is None:
  145. frappe.local.user_format = frappe.db.get_default("date_format")
  146. return frappe.local.user_format or "yyyy-mm-dd"
  147. def formatdate(string_date=None, format_string=None):
  148. """
  149. Convers the given string date to :data:`user_format`
  150. User format specified in defaults
  151. Examples:
  152. * dd-mm-yyyy
  153. * mm-dd-yyyy
  154. * dd/mm/yyyy
  155. """
  156. date = getdate(string_date) if string_date else now_datetime().date()
  157. if not format_string:
  158. format_string = get_user_format().replace("mm", "MM")
  159. return babel.dates.format_date(date, format_string, locale=(frappe.local.lang or "").replace("-", "_"))
  160. def format_time(txt):
  161. return babel.dates.format_time(get_time(txt), locale=(frappe.local.lang or "").replace("-", "_"))
  162. def format_datetime(datetime_string, format_string=None):
  163. if not datetime_string:
  164. return
  165. datetime = get_datetime(datetime_string)
  166. if not format_string:
  167. format_string = get_user_format().replace("mm", "MM") + " HH:mm:ss"
  168. return babel.dates.format_datetime(datetime, format_string, locale=(frappe.local.lang or "").replace("-", "_"))
  169. def global_date_format(date):
  170. """returns date as 1 January 2012"""
  171. formatted_date = getdate(date).strftime("%d %B %Y")
  172. return formatted_date.startswith("0") and formatted_date[1:] or formatted_date
  173. def has_common(l1, l2):
  174. """Returns truthy value if there are common elements in lists l1 and l2"""
  175. return set(l1) & set(l2)
  176. def flt(s, precision=None):
  177. """Convert to float (ignore commas)"""
  178. if isinstance(s, basestring):
  179. s = s.replace(',','')
  180. try:
  181. num = float(s)
  182. if precision is not None:
  183. num = rounded(num, precision)
  184. except Exception:
  185. num = 0
  186. return num
  187. def cint(s):
  188. """Convert to integer"""
  189. try: num = int(float(s))
  190. except: num = 0
  191. return num
  192. def cstr(s):
  193. if isinstance(s, unicode):
  194. return s
  195. elif s==None:
  196. return ''
  197. elif isinstance(s, basestring):
  198. return unicode(s, 'utf-8')
  199. else:
  200. return unicode(s)
  201. def rounded(num, precision=0):
  202. """round method for round halfs to nearest even algorithm aka banker's rounding - compatible with python3"""
  203. precision = cint(precision)
  204. multiplier = 10 ** precision
  205. # avoid rounding errors
  206. num = round(num * multiplier if precision else num, 8)
  207. floor = math.floor(num)
  208. decimal_part = num - floor
  209. if not precision and decimal_part == 0.5:
  210. num = floor if (floor % 2 == 0) else floor + 1
  211. else:
  212. num = round(num)
  213. return (num / multiplier) if precision else num
  214. def remainder(numerator, denominator, precision=2):
  215. precision = cint(precision)
  216. multiplier = 10 ** precision
  217. if precision:
  218. _remainder = ((numerator * multiplier) % (denominator * multiplier)) / multiplier
  219. else:
  220. _remainder = numerator % denominator
  221. return flt(_remainder, precision);
  222. def round_based_on_smallest_currency_fraction(value, currency, precision=2):
  223. smallest_currency_fraction_value = flt(frappe.db.get_value("Currency",
  224. currency, "smallest_currency_fraction_value"))
  225. if smallest_currency_fraction_value:
  226. remainder_val = remainder(value, smallest_currency_fraction_value, precision)
  227. if remainder_val > (smallest_currency_fraction_value / 2):
  228. value += smallest_currency_fraction_value - remainder_val
  229. else:
  230. value -= remainder_val
  231. else:
  232. value = rounded(value)
  233. return flt(value, precision)
  234. def encode(obj, encoding="utf-8"):
  235. if isinstance(obj, list):
  236. out = []
  237. for o in obj:
  238. if isinstance(o, unicode):
  239. out.append(o.encode(encoding))
  240. else:
  241. out.append(o)
  242. return out
  243. elif isinstance(obj, unicode):
  244. return obj.encode(encoding)
  245. else:
  246. return obj
  247. def parse_val(v):
  248. """Converts to simple datatypes from SQL query results"""
  249. if isinstance(v, (datetime.date, datetime.datetime)):
  250. v = unicode(v)
  251. elif isinstance(v, datetime.timedelta):
  252. v = ":".join(unicode(v).split(":")[:2])
  253. elif isinstance(v, long):
  254. v = int(v)
  255. return v
  256. def fmt_money(amount, precision=None, currency=None):
  257. """
  258. Convert to string with commas for thousands, millions etc
  259. """
  260. number_format = None
  261. if currency:
  262. number_format = frappe.db.get_value("Currency", currency, "number_format", cache=True)
  263. if not number_format:
  264. number_format = frappe.db.get_default("number_format") or "#,###.##"
  265. decimal_str, comma_str, number_format_precision = get_number_format_info(number_format)
  266. if precision is None:
  267. precision = number_format_precision
  268. amount = '%.*f' % (precision, flt(amount))
  269. if amount.find('.') == -1:
  270. decimals = ''
  271. else:
  272. decimals = amount.split('.')[1]
  273. parts = []
  274. minus = ''
  275. if flt(amount) < 0:
  276. minus = '-'
  277. amount = cstr(abs(flt(amount))).split('.')[0]
  278. if len(amount) > 3:
  279. parts.append(amount[-3:])
  280. amount = amount[:-3]
  281. val = number_format=="#,##,###.##" and 2 or 3
  282. while len(amount) > val:
  283. parts.append(amount[-val:])
  284. amount = amount[:-val]
  285. parts.append(amount)
  286. parts.reverse()
  287. amount = comma_str.join(parts) + ((precision and decimal_str) and (decimal_str + decimals) or "")
  288. amount = minus + amount
  289. if currency and frappe.defaults.get_global_default("hide_currency_symbol") != "Yes":
  290. symbol = frappe.db.get_value("Currency", currency, "symbol") or currency
  291. amount = symbol + " " + amount
  292. return amount
  293. number_format_info = {
  294. "#,###.##": (".", ",", 2),
  295. "#.###,##": (",", ".", 2),
  296. "# ###.##": (".", " ", 2),
  297. "# ###,##": (",", " ", 2),
  298. "#'###.##": (".", "'", 2),
  299. "#, ###.##": (".", ", ", 2),
  300. "#,##,###.##": (".", ",", 2),
  301. "#,###.###": (".", ",", 3),
  302. "#.###": ("", ".", 0),
  303. "#,###": ("", ",", 0)
  304. }
  305. def get_number_format_info(format):
  306. return number_format_info.get(format) or (".", ",", 2)
  307. #
  308. # convet currency to words
  309. #
  310. def money_in_words(number, main_currency = None, fraction_currency=None):
  311. """
  312. Returns string in words with currency and fraction currency.
  313. """
  314. from frappe.utils import get_defaults
  315. _ = frappe._
  316. if not number or flt(number) < 0:
  317. return ""
  318. d = get_defaults()
  319. if not main_currency:
  320. main_currency = d.get('currency', 'INR')
  321. if not fraction_currency:
  322. fraction_currency = frappe.db.get_value("Currency", main_currency, "fraction") or _("Cent")
  323. n = "%.2f" % flt(number)
  324. main, fraction = n.split('.')
  325. if len(fraction)==1: fraction += '0'
  326. number_format = frappe.db.get_value("Currency", main_currency, "number_format", cache=True) or \
  327. frappe.db.get_default("number_format") or "#,###.##"
  328. in_million = True
  329. if number_format == "#,##,###.##": in_million = False
  330. out = main_currency + ' ' + in_words(main, in_million).title()
  331. if cint(fraction):
  332. out = out + ' ' + _('and') + ' ' + in_words(fraction, in_million).title() + ' ' + fraction_currency
  333. return out + ' ' + _('only.')
  334. #
  335. # convert number to words
  336. #
  337. def in_words(integer, in_million=True):
  338. """
  339. Returns string in words for the given integer.
  340. """
  341. locale = 'en_IN' if not in_million else frappe.local.lang
  342. integer = int(integer)
  343. try:
  344. ret = num2words(integer, lang=locale)
  345. except NotImplementedError:
  346. ret = num2words(integer, lang='en')
  347. return ret.replace('-', ' ')
  348. def is_html(text):
  349. out = False
  350. for key in ["<br>", "<p", "<img", "<div"]:
  351. if key in text:
  352. out = True
  353. break
  354. return out
  355. # from Jinja2 code
  356. _striptags_re = re.compile(r'(<!--.*?-->|<[^>]*>)')
  357. def strip_html(text):
  358. """removes anything enclosed in and including <>"""
  359. return _striptags_re.sub("", text)
  360. def escape_html(text):
  361. html_escape_table = {
  362. "&": "&amp;",
  363. '"': "&quot;",
  364. "'": "&apos;",
  365. ">": "&gt;",
  366. "<": "&lt;",
  367. }
  368. return "".join(html_escape_table.get(c,c) for c in text)
  369. def pretty_date(iso_datetime):
  370. """
  371. Takes an ISO time and returns a string representing how
  372. long ago the date represents.
  373. Ported from PrettyDate by John Resig
  374. """
  375. if not iso_datetime: return ''
  376. import math
  377. if isinstance(iso_datetime, basestring):
  378. iso_datetime = datetime.datetime.strptime(iso_datetime, DATETIME_FORMAT)
  379. now_dt = datetime.datetime.strptime(now(), DATETIME_FORMAT)
  380. dt_diff = now_dt - iso_datetime
  381. # available only in python 2.7+
  382. # dt_diff_seconds = dt_diff.total_seconds()
  383. dt_diff_seconds = dt_diff.days * 86400.0 + dt_diff.seconds
  384. dt_diff_days = math.floor(dt_diff_seconds / 86400.0)
  385. # differnt cases
  386. if dt_diff_seconds < 60.0:
  387. return 'just now'
  388. elif dt_diff_seconds < 120.0:
  389. return '1 minute ago'
  390. elif dt_diff_seconds < 3600.0:
  391. return '%s minutes ago' % cint(math.floor(dt_diff_seconds / 60.0))
  392. elif dt_diff_seconds < 7200.0:
  393. return '1 hour ago'
  394. elif dt_diff_seconds < 86400.0:
  395. return '%s hours ago' % cint(math.floor(dt_diff_seconds / 3600.0))
  396. elif dt_diff_days == 1.0:
  397. return 'Yesterday'
  398. elif dt_diff_days < 7.0:
  399. return '%s days ago' % cint(dt_diff_days)
  400. elif dt_diff_days < 31.0:
  401. return '%s week(s) ago' % cint(math.ceil(dt_diff_days / 7.0))
  402. elif dt_diff_days < 365.0:
  403. return '%s months ago' % cint(math.ceil(dt_diff_days / 30.0))
  404. else:
  405. return 'more than %s year(s) ago' % cint(math.floor(dt_diff_days / 365.0))
  406. def comma_or(some_list):
  407. return comma_sep(some_list, frappe._("{0} or {1}"))
  408. def comma_and(some_list):
  409. return comma_sep(some_list, frappe._("{0} and {1}"))
  410. def comma_sep(some_list, pattern):
  411. if isinstance(some_list, (list, tuple)):
  412. # list(some_list) is done to preserve the existing list
  413. some_list = [unicode(s) for s in list(some_list)]
  414. if not some_list:
  415. return ""
  416. elif len(some_list) == 1:
  417. return some_list[0]
  418. else:
  419. some_list = ["'%s'" % s for s in some_list]
  420. return pattern.format(", ".join(frappe._(s) for s in some_list[:-1]), some_list[-1])
  421. else:
  422. return some_list
  423. def new_line_sep(some_list):
  424. if isinstance(some_list, (list, tuple)):
  425. # list(some_list) is done to preserve the existing list
  426. some_list = [unicode(s) for s in list(some_list)]
  427. if not some_list:
  428. return ""
  429. elif len(some_list) == 1:
  430. return some_list[0]
  431. else:
  432. some_list = ["%s" % s for s in some_list]
  433. return format("\n ".join(some_list))
  434. else:
  435. return some_list
  436. def filter_strip_join(some_list, sep):
  437. """given a list, filter None values, strip spaces and join"""
  438. return (cstr(sep)).join((cstr(a).strip() for a in filter(None, some_list)))
  439. def get_url(uri=None, full_address=False):
  440. """get app url from request"""
  441. host_name = frappe.local.conf.host_name or frappe.local.conf.hostname
  442. if uri and (uri.startswith("http://") or uri.startswith("https://")):
  443. return uri
  444. if not host_name:
  445. if hasattr(frappe.local, "request") and frappe.local.request and frappe.local.request.host:
  446. protocol = 'https' == frappe.get_request_header('X-Forwarded-Proto', "") and 'https://' or 'http://'
  447. host_name = protocol + frappe.local.request.host
  448. elif frappe.local.site:
  449. protocol = 'http://'
  450. if frappe.local.conf.ssl_certificate:
  451. protocol = 'https://'
  452. elif frappe.local.conf.wildcard:
  453. domain = frappe.local.conf.wildcard.get('domain')
  454. if domain and frappe.local.site.endswith(domain) and frappe.local.conf.wildcard.get('ssl_certificate'):
  455. protocol = 'https://'
  456. host_name = protocol + frappe.local.site
  457. else:
  458. host_name = frappe.db.get_value("Website Settings", "Website Settings",
  459. "subdomain")
  460. if host_name and "http" not in host_name:
  461. host_name = "http://" + host_name
  462. if not host_name:
  463. host_name = "http://localhost"
  464. if not uri and full_address:
  465. uri = frappe.get_request_header("REQUEST_URI", "")
  466. url = urllib.basejoin(host_name, uri) if uri else host_name
  467. return url
  468. def get_host_name():
  469. return get_url().rsplit("//", 1)[-1]
  470. def get_link_to_form(doctype, name, label=None):
  471. if not label: label = name
  472. return """<a href="{0}">{1}</a>""".format(get_url_to_form(doctype, name), label)
  473. def get_url_to_form(doctype, name):
  474. return get_url(uri = "desk#Form/{0}/{1}".format(quoted(doctype), quoted(name)))
  475. def get_url_to_list(doctype):
  476. return get_url(uri = "desk#List/{0}".format(quoted(doctype)))
  477. operator_map = {
  478. # startswith
  479. "^": lambda (a, b): (a or "").startswith(b),
  480. # in or not in a list
  481. "in": lambda (a, b): operator.contains(b, a),
  482. "not in": lambda (a, b): not operator.contains(b, a),
  483. # comparison operators
  484. "=": lambda (a, b): operator.eq(a, b),
  485. "!=": lambda (a, b): operator.ne(a, b),
  486. ">": lambda (a, b): operator.gt(a, b),
  487. "<": lambda (a, b): operator.lt(a, b),
  488. ">=": lambda (a, b): operator.ge(a, b),
  489. "<=": lambda (a, b): operator.le(a, b),
  490. "not None": lambda (a, b): a and True or False,
  491. "None": lambda (a, b): (not a) and True or False
  492. }
  493. def evaluate_filters(doc, filters):
  494. '''Returns true if doc matches filters'''
  495. if isinstance(filters, dict):
  496. for key, value in filters.iteritems():
  497. f = get_filter(None, {key:value})
  498. if not compare(doc.get(f.fieldname), f.operator, f.value):
  499. return False
  500. elif isinstance(filters, (list, tuple)):
  501. for d in filters:
  502. f = get_filter(None, d)
  503. if not compare(doc.get(f.fieldname), f.operator, f.value):
  504. return False
  505. return True
  506. def compare(val1, condition, val2):
  507. ret = False
  508. if condition in operator_map:
  509. ret = operator_map[condition]((val1, val2))
  510. return ret
  511. def get_filter(doctype, f):
  512. """Returns a _dict like
  513. {
  514. "doctype":
  515. "fieldname":
  516. "operator":
  517. "value":
  518. }
  519. """
  520. from frappe.model import default_fields, optional_fields
  521. if isinstance(f, dict):
  522. key, value = f.items()[0]
  523. f = make_filter_tuple(doctype, key, value)
  524. if not isinstance(f, (list, tuple)):
  525. frappe.throw("Filter must be a tuple or list (in a list)")
  526. if len(f) == 3:
  527. f = (doctype, f[0], f[1], f[2])
  528. elif len(f) != 4:
  529. frappe.throw("Filter must have 4 values (doctype, fieldname, operator, value): {0}".format(str(f)))
  530. f = frappe._dict(doctype=f[0], fieldname=f[1], operator=f[2], value=f[3])
  531. if not f.operator:
  532. # if operator is missing
  533. f.operator = "="
  534. valid_operators = ("=", "!=", ">", "<", ">=", "<=", "like", "not like", "in", "not in")
  535. if f.operator not in valid_operators:
  536. frappe.throw("Operator must be one of {0}".format(", ".join(valid_operators)))
  537. if f.doctype and (f.fieldname not in default_fields + optional_fields):
  538. # verify fieldname belongs to the doctype
  539. meta = frappe.get_meta(f.doctype)
  540. if not meta.has_field(f.fieldname):
  541. # try and match the doctype name from child tables
  542. for df in meta.get_table_fields():
  543. if frappe.get_meta(df.options).has_field(f.fieldname):
  544. f.doctype = df.options
  545. break
  546. return f
  547. def make_filter_tuple(doctype, key, value):
  548. '''return a filter tuple like [doctype, key, operator, value]'''
  549. if isinstance(value, (list, tuple)):
  550. return [doctype, key, value[0], value[1]]
  551. else:
  552. return [doctype, key, "=", value]
  553. def scrub_urls(html):
  554. html = expand_relative_urls(html)
  555. # encoding should be responsibility of the composer
  556. # html = quote_urls(html)
  557. return html
  558. def expand_relative_urls(html):
  559. # expand relative urls
  560. url = get_url()
  561. if url.endswith("/"): url = url[:-1]
  562. def _expand_relative_urls(match):
  563. to_expand = list(match.groups())
  564. if not to_expand[2].startswith("/"):
  565. to_expand[2] = "/" + to_expand[2]
  566. to_expand.insert(2, url)
  567. if 'url' in to_expand[0] and to_expand[1].startswith('(') and to_expand[-1].endswith(')'):
  568. # background-image: url('/assets/...') - workaround for wkhtmltopdf print-media-type
  569. to_expand.append(' !important')
  570. return "".join(to_expand)
  571. html = re.sub('(href|src){1}([\s]*=[\s]*[\'"]?)((?!http)[^\'" >]+)([\'"]?)', _expand_relative_urls, html)
  572. # background-image: url('/assets/...')
  573. html = re.sub('(:[\s]?url)(\([\'"]?)([^\)]*)([\'"]?\))', _expand_relative_urls, html)
  574. return html
  575. def quoted(url):
  576. return cstr(urllib.quote(encode(url), safe=b"~@#$&()*!+=:;,.?/'"))
  577. def quote_urls(html):
  578. def _quote_url(match):
  579. groups = list(match.groups())
  580. groups[2] = quoted(groups[2])
  581. return "".join(groups)
  582. return re.sub('(href|src){1}([\s]*=[\s]*[\'"]?)((?:http)[^\'">]+)([\'"]?)',
  583. _quote_url, html)
  584. def unique(seq):
  585. """use this instead of list(set()) to preserve order of the original list.
  586. Thanks to Stackoverflow: http://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-in-python-whilst-preserving-order"""
  587. seen = set()
  588. seen_add = seen.add
  589. return [ x for x in seq if not (x in seen or seen_add(x)) ]
  590. def strip(val, chars=None):
  591. # \ufeff is no-width-break, \u200b is no-width-space
  592. return (val or "").replace("\ufeff", "").replace("\u200b", "").strip(chars)
  593. def to_markdown(html):
  594. text = None
  595. try:
  596. text = html2text(html)
  597. except HTMLParser.HTMLParseError:
  598. pass
  599. return text