astoff / devdocs.el

Emacs viewer for DevDocs
297 stars 17 forks source link

Relative links to parent directories #3

Closed kljohann closed 3 years ago

kljohann commented 3 years ago

Currently links starting with ../ do not work (even if they do not leave the documentation directory). E.g., for the link on http://en.cppreference.com/w/cpp/keyword/asm, (devdocs--path-expand "../language/asm" "keyword/asm") should return language/asm instead of keyword/../language/asm.

I use the following as a workaround, but there is likely a better way to fix this.

@@ -272,7 +272,14 @@ (defun devdocs--path-expand (path base)
   (pcase (string-to-char path)
     ('?/ path)
     ('?# (concat (devdocs--path-file base) path))
-    (_ (concat (file-name-directory base) path))))
+    (_
+     (setq base (file-name-directory base))
+     (while (string-prefix-p "../" path)
+       (unless base
+         (error "Path escapes documentation directory: %s" path))
+       (setq path (string-trim-left path (rx "../"))
+             base (file-name-directory (directory-file-name base))))
+     (concat (or base "") path))))

 (defun devdocs--shr-tag-pre (dom)
astoff commented 3 years ago

Yes, I noticed this randomly at some point. Thanks for pointing out to a specific example!