summaryrefslogtreecommitdiff
path: root/ldp/path.scm
diff options
context:
space:
mode:
Diffstat (limited to 'ldp/path.scm')
-rw-r--r--ldp/path.scm64
1 files changed, 64 insertions, 0 deletions
diff --git a/ldp/path.scm b/ldp/path.scm
new file mode 100644
index 0000000..fb25ba2
--- /dev/null
+++ b/ldp/path.scm
@@ -0,0 +1,64 @@
+(define-module (ldp path)
+ #:use-module (oop goops)
+ #:use-module (web uri))
+
+(define-class <path> ()
+ (components-rev #:init-keyword #:components-rev #:getter path-components-rev))
+
+(export <path>)
+
+(define-public (is-root? x)
+ (equal? (path-components-rev x) '()))
+
+(define-public (path->filename x)
+ (if (is-root? x)
+ "."
+ (string-append
+ "./"
+ (encode-and-join-uri-path
+ (map (lambda (x)
+ (string-append "r_" x))
+ (reverse (path-components-rev x)))))))
+
+(define-public (path->uri x)
+ (let* ((components (reverse (path-components-rev x)))
+ (relative-to-root (encode-and-join-uri-path components))
+ (path (string-append "/" relative-to-root)))
+ (build-uri-reference #:path path)))
+
+(define-public (path->string x)
+ (uri-path (path->uri x)))
+
+(define-public (uri->path x)
+ (make <path>
+ #:components-rev
+ (reverse (split-and-decode-uri-path (uri-path x)))))
+
+(define-public (string->path x)
+ (uri->path (build-uri-reference #:path x)))
+
+(define-public (path? x)
+ (is-a? x <path>))
+
+(define-public (the-path x)
+ (unless (path? x)
+ (scm-error 'wrong-type-arg
+ "the-path"
+ "Expected a path from (ldp path)."
+ '()
+ (list x)))
+ x)
+
+(define-public (path-parent x)
+ (make <path> #:components-rev (cdr (path-components-rev x))))
+
+(define-public (path-cons container slug)
+ (make <path>
+ #:components-rev
+ (cons slug (path-components-rev container))))
+
+(define-public (path-slug x)
+ (car (path-components-rev x)))
+
+(define-public (path-equal? x y)
+ (equal? (path-components-rev x) (path-components-rev y)))