~reesmichael1/roman

b0b0bdc60a9b641fb03a7f754bb68b4e0832b9bd — Michael Rees 8 months ago 3df822e
Add field to config file to control post width
4 files changed, 26 insertions(+), 1 deletions(-)

M config/config
M src/romanpkg/config.nim
M src/romanpkg/htmlextractor.nim
M src/romanpkg/types.nim
M config/config => config/config +6 -0
@@ 6,3 6,9 @@ up=k
next=n
previous=p
quit=q

[Posts]
# The maximum width for wrapping the posts
# If this is greater than the terminal width, 
# words will be wrapped to match the terminal isntead.
max-width=80

M src/romanpkg/config.nim => src/romanpkg/config.nim +15 -0
@@ 1,5 1,6 @@
import parsecfg
import sequtils
import strutils

import errors
import paths


@@ 20,6 21,19 @@ proc strToChar(config: Config, section: string, key: string): char {.
      "missing config value for " & section & "." & key)


proc strToInt(config: Config, section: string, key: string): int {.
    raises: [RomanError].} =
  try:
    let s = config.getSectionValue(section, key)
    result = parseInt(s)
  except KeyError:
    raise newException(RomanError,
      "missing config value for " & section & "." & key)
  except ValueError:
    raise newException(RomanError,
      "invalid value for " & section & "." & key & ", expected int")


proc mustLoadConfig*(): RomanConfig {.raises: [].} =
  try:
    let path = getConfigFilePath()


@@ 29,6 43,7 @@ proc mustLoadConfig*(): RomanConfig {.raises: [].} =
    result.next = strToChar(dict, "Keyboard", "next")
    result.previous = strToChar(dict, "Keyboard", "previous")
    result.quit = strToChar(dict, "Keyboard", "quit")
    result.postWidth = strToInt(dict, "Posts", "max-width")
  except:
    echo "error loading config file: " & getCurrentExceptionMsg()
    quit(1)

M src/romanpkg/htmlextractor.nim => src/romanpkg/htmlextractor.nim +4 -1
@@ 5,10 5,13 @@ import nimpy

import errors

from config import conf


proc extractBody*(body: string): string {.raises: [RomanError].} =
  try:
    let html2text = pyImport("html2text").HTML2Text()
    let width = min(conf.postWidth, terminalWidth())
    let html2text = pyImport("html2text").HTML2Text(bodywidth = width)
    result = html2text.handle(body).to(string)
  except:
    raise newException(RomanError, "could not use Python module html2text")

M src/romanpkg/types.nim => src/romanpkg/types.nim +1 -0
@@ 5,6 5,7 @@ type
    next*: char
    previous*: char
    quit*: char
    postWidth*: int

  Subscription* = object
    url*: string