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
|
const
|
||||||
allowedExts = @[".org"]
|
allowedExts = @[".org"]
|
||||||
|
orgRegex = r"\[\[(.+)\]\[(.+)\]\]"
|
||||||
|
|
||||||
proc scanDir*(dir: string): seq[string] =
|
proc newLink(href, text, file: string): Link =
|
||||||
## Walk the file system under the root dir and collect all files that could contain links
|
Link(href: href, text: text, file: file)
|
||||||
setCurrentDir(dir)
|
|
||||||
|
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]
|
var parts: tuple[dir, name, ext: string]
|
||||||
for entry in walkDirRec(getCurrentDir(), relative = true):
|
for entry in walkDirRec(getCurrentDir(), relative = true):
|
||||||
if fileExists(entry):
|
if fileExists(entry):
|
||||||
@@ -13,11 +24,21 @@ proc scanDir*(dir: string): seq[string] =
|
|||||||
if parts.ext in allowedExts:
|
if parts.ext in allowedExts:
|
||||||
result.add(entry)
|
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:
|
when isMainModule:
|
||||||
let args = commandLineParams()
|
let args = commandLineParams()
|
||||||
var root = "./"
|
var root = "./"
|
||||||
if args.len == 1:
|
if args.len == 1:
|
||||||
root = args[0]
|
root = args[0]
|
||||||
let files = scanDir(root)
|
setCurrentDir(root)
|
||||||
|
let files = scanDir()
|
||||||
echo "Found these files:\n", files
|
echo "Found these files:\n", files
|
||||||
|
|
||||||
|
|||||||
@@ -2,17 +2,18 @@ import unittest, os
|
|||||||
import lamv
|
import lamv
|
||||||
|
|
||||||
suite "Test the file detection":
|
suite "Test the file detection":
|
||||||
setup:
|
const root = "example"
|
||||||
const root = "./example"
|
setCurrentDir(root)
|
||||||
|
|
||||||
test "Directory does not exist":
|
|
||||||
expect(OSError):
|
|
||||||
discard scanDir("./ThisDirDoesNotExist")
|
|
||||||
|
|
||||||
test "Find org files":
|
test "Find org files":
|
||||||
let files = scanDir(root)
|
let files = scanDir()
|
||||||
var parts: tuple[dir, name, ext: string]
|
var parts: tuple[dir, name, ext: string]
|
||||||
check files.len > 1
|
check files.len > 1
|
||||||
for file in files:
|
for file in files:
|
||||||
parts = splitFile(file)
|
parts = splitFile(file)
|
||||||
check parts.ext == ".org"
|
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