From 7c408c0f86c5d42230c0b27a50e892345fa32844 Mon Sep 17 00:00:00 2001 From: luxick Date: Sat, 15 Feb 2020 16:49:24 +0100 Subject: [PATCH] Link parser --- src/lamv.nim | 31 ++++++++++++++++++++++++++----- tests/testDetection.nim | 17 +++++++++-------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/lamv.nim b/src/lamv.nim index f035a98..0d0e194 100644 --- a/src/lamv.nim +++ b/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 diff --git a/tests/testDetection.nim b/tests/testDetection.nim index a9bce04..de1009f 100644 --- a/tests/testDetection.nim +++ b/tests/testDetection.nim @@ -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" \ No newline at end of file + check parts.ext == ".org" + + test "Find links in file": + let files = scanDir() + for file in files: + discard file.parseLinks() \ No newline at end of file