~reesmichael1/roman

3d66b40d22dbea164a2e2e244f542699e1ea5e24 — Michael Rees 1 year, 3 months ago a346a4d
Add ability to open links in posts in browser
M config/config => config/config +1 -0
@@ 14,3 14,4 @@ bottom=G
# If this is greater than the terminal width, 
# words will be wrapped to match the terminal isntead.
max-width=80
extract-links=L

M src/romanpkg/config.nim => src/romanpkg/config.nim +1 -0
@@ 46,6 46,7 @@ proc mustLoadConfig*(): RomanConfig {.raises: [].} =
    result.goToTop = strToChar(dict, "Keyboard", "top")
    result.goToBottom = strToChar(dict, "Keyboard", "bottom")
    result.postWidth = strToInt(dict, "Posts", "max-width")
    result.extractLInks = strToChar(dict, "Posts", "extract-links")
  except:
    echo "error loading config file: " & getCurrentExceptionMsg()
    quit(1)

M src/romanpkg/pager.nim => src/romanpkg/pager.nim +5 -1
@@ 52,7 52,9 @@ proc wrapLines(contents: string, width: int): seq[string] =


proc page*(contents: string, goToBottom = 'G', goToTop = 'g', upOne = 'k',
    quitChar = 'q', downOne = 'j', upHalf = chr(21), downHalf = chr(4)) =
    quitChar = 'q', downOne = 'j', upHalf = chr(21), downHalf = chr(4),
    extractLinks = 'L', extractLinksProc = (proc() {.closure,
        locks: "unknown".} = return)) =
  hideCursor(stdout)
  # lineIx is the index of the *top* line that should be shown
  var lineIx = 0


@@ 95,6 97,8 @@ proc page*(contents: string, goToBottom = 'G', goToTop = 'g', upOne = 'k',
      lineIx = max(0, lineIx - stepSize)
    elif c == downHalf: # Ctrl-D: go down one half-screen's worth
      lineIx = min(lineIx + stepSize, wrappedLines.len - height)
    elif c == extractLinks:
      extractLinksProc()
    elif c == '\3' or c == quitChar: # keyboard interrupt or quit
      stdout.write "\n"
      showCursor(stdout)

M src/romanpkg/posts.nim => src/romanpkg/posts.nim +16 -9
@@ 1,8 1,7 @@
import browsers
import htmlparser
import options
import os
import strtabs
import strutils
import terminal
import xmltree



@@ 12,6 11,7 @@ import pager
import errors
import htmlextractor
import paths
import termask

from config import conf
from types import Post


@@ 61,11 61,17 @@ proc displayLinks(content: string) {.raises: [RomanError].} =
      links.add(a.attrs.getOrDefault("href"))

  if links.len > 0:
    echo "\nLinks found in post:"
    for link in links:
      echo link

    echo "\n"
    try:
      let link = promptList("Select link to open in system browser", links).get
      openDefaultBrowser(link)
    except ValueError:
      discard
    except UnpackError:
      discard
    except IOError as e:
      raise newException(RomanError, "could not display links: " & e.msg)
    except Exception as e:
      raise newException(RomanError, "could not open link: " & e.msg)


proc displayPost*(p: Post) {.raises: [RomanError].} =


@@ 76,8 82,9 @@ proc displayPost*(p: Post) {.raises: [RomanError].} =
    else:
      content = p.title & "\n\n" & p.rendered
    page(content, goToBottom = conf.goToBottom, goToTop = conf.goToTop,
      upOne = conf.up, downOne = conf.down, quitChar = conf.quit)
    displayLinks(p.raw)
      upOne = conf.up, downOne = conf.down, quitChar = conf.quit,
      extractLinks = conf.extractLinks, extractLinksProc = proc() {.closure,
          noSideEffect, gcsafe.} = {.noSideEffect.}: displayLinks(p.raw))
  except IOError, ValueError:
    let msg = getCurrentExceptionMsg()
    raise newException(RomanError, "could not write to the terminal: " & msg)

M src/romanpkg/types.nim => src/romanpkg/types.nim +1 -0
@@ 11,6 11,7 @@ type
    goToTop*: char
    goToBottom*: char
    postWidth*: int
    extractLinks*: char

  Subscription* = object
    url*: string