~reesmichael1/roman

ref: 3784df6eb213fc0ccd683ed396d42645e1bca718 roman/src/romanpkg/htmlextractor.nim -rw-r--r-- 796 bytes
3784df6eMichael Rees Fix crash when displaying very long link in post 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import terminal

import errors
when defined(internalRenderer):
  import html2text
else:
  import nimpy


from config import conf


proc extractBody*(body: string): string {.raises: [RomanError].} =
  let width = try:
    min(conf.postWidth, terminalWidth())
  except:
    raise newException(RomanError, "could not get terminal width")
  try:
    when defined(internalRenderer):
      # Use nim-html2text if -d:internalRenderer is passed
      return handle(body, maxWidth = width)

    else:
      # Otherwise, use the Python version (which needs to be installed)
      let html2text = pyImport("html2text").HTML2Text(bodywidth = width)
      return html2text.handle(body).to(string)
  except:
    raise newException(RomanError, "error rendering HTML to text: " &
      getCurrentExceptionMsg())