Link parser
This commit is contained in:
31
src/lamv.nim
31
src/lamv.nim
@@ -1,11 +1,22 @@
|
||||
import os
|
||||
import os, re
|
||||
|
||||
type
|
||||
Link = object
|
||||
## Represents a link in a text file
|
||||
href: string ## The link part that connects to another file
|
||||
text: string ## The text part of the link
|
||||
file: string ## The file, the link is found in
|
||||
|
||||
const
|
||||
allowedExts = @[".org"]
|
||||
orgRegex = r"\[\[(.+)\]\[(.+)\]\]"
|
||||
|
||||
proc scanDir*(dir: string): seq[string] =
|
||||
## Walk the file system under the root dir and collect all files that could contain links
|
||||
setCurrentDir(dir)
|
||||
proc newLink(href, text, file: string): Link =
|
||||
Link(href: href, text: text, file: file)
|
||||
|
||||
proc scanDir*(): seq[string] =
|
||||
## Walk the file system under the root dir
|
||||
## and collect all files that could contain links
|
||||
var parts: tuple[dir, name, ext: string]
|
||||
for entry in walkDirRec(getCurrentDir(), relative = true):
|
||||
if fileExists(entry):
|
||||
@@ -13,11 +24,21 @@ proc scanDir*(dir: string): seq[string] =
|
||||
if parts.ext in allowedExts:
|
||||
result.add(entry)
|
||||
|
||||
proc parseLinks*(file: string): seq[Link] =
|
||||
## Parses the given text file and returns all links in the file
|
||||
let f = open(file)
|
||||
for line in f.lines:
|
||||
let matches = line.findAll(re(orgRegex))
|
||||
for match in matches:
|
||||
echo "Link: ", match, " in file ", file
|
||||
|
||||
|
||||
when isMainModule:
|
||||
let args = commandLineParams()
|
||||
var root = "./"
|
||||
if args.len == 1:
|
||||
root = args[0]
|
||||
let files = scanDir(root)
|
||||
setCurrentDir(root)
|
||||
let files = scanDir()
|
||||
echo "Found these files:\n", files
|
||||
|
||||
|
||||
@@ -2,17 +2,18 @@ import unittest, os
|
||||
import lamv
|
||||
|
||||
suite "Test the file detection":
|
||||
setup:
|
||||
const root = "./example"
|
||||
|
||||
test "Directory does not exist":
|
||||
expect(OSError):
|
||||
discard scanDir("./ThisDirDoesNotExist")
|
||||
const root = "example"
|
||||
setCurrentDir(root)
|
||||
|
||||
test "Find org files":
|
||||
let files = scanDir(root)
|
||||
let files = scanDir()
|
||||
var parts: tuple[dir, name, ext: string]
|
||||
check files.len > 1
|
||||
for file in files:
|
||||
parts = splitFile(file)
|
||||
check parts.ext == ".org"
|
||||
|
||||
test "Find links in file":
|
||||
let files = scanDir()
|
||||
for file in files:
|
||||
discard file.parseLinks()
|
||||
Reference in New Issue
Block a user