~reesmichael1/roman

ref: 936ef3138ad86e28536ce252d4f10c02e045759e roman/src/romanpkg/htmlextractor.nim -rw-r--r-- 788 bytes View raw
936ef313Michael Rees Add option for using nim-html2text 2 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
import terminal

import nimpy

import errors
when defined(internalRenderer):
  import html2text

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())