about summary refs log tree commit diff stats
path: root/adapter/protocol/man.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-03-15 12:18:45 +0100
committerbptato <nincsnevem662@gmail.com>2024-03-15 12:18:45 +0100
commit6391a9a2abde4e6a681cb0a1a1831d0c1b87026a (patch)
tree89f7278e649c74f598c210506cce247ed97e2f86 /adapter/protocol/man.nim
parent39ae15c1049f41f0ab3b60e420fa6d79f5ed48cd (diff)
downloadchawan-6391a9a2abde4e6a681cb0a1a1831d0c1b87026a.tar.gz
man: improve header/footer conversion
* run processBackspace on the first line, because groff likes to print
  formatting there too
* check man references like SAMEPAGE(1) with isCommand because it's
  commonly found in footers
Diffstat (limited to 'adapter/protocol/man.nim')
-rw-r--r--adapter/protocol/man.nim15
1 files changed, 11 insertions, 4 deletions
diff --git a/adapter/protocol/man.nim b/adapter/protocol/man.nim
index 17f44dea..f8aa6ad8 100644
--- a/adapter/protocol/man.nim
+++ b/adapter/protocol/man.nim
@@ -98,7 +98,7 @@ iterator myCaptures(captures: var seq[RegexCapture]; target: int;
       cap.e += offset
       yield cap
 
-proc processManpage(file: File; header: string) =
+proc processManpage(file: File; header, keyword: string) =
   var line: string
   # The "right thing" would be to check for the error code and output error
   # messages accordingly. Unfortunately that would prevent us from streaming
@@ -123,7 +123,7 @@ proc processManpage(file: File; header: string) =
   # this is useful because otherwise the header would get caught in the man
   # regex, and that makes navigation slightly more annoying
   stdout.write(header)
-  stdout.write(line & '\n')
+  stdout.write(line.processBackspace() & '\n')
   var wasBlank = false
   template re(s: static string): Regex =
     let r = s.compileRegex({LRE_FLAG_GLOBAL, LRE_FLAG_UTF16})
@@ -138,6 +138,9 @@ proc processManpage(file: File; header: string) =
   let includeRe = re"#include(</?[bu]>|\s)*&lt;([\w./-]+)"
   let manRe = re"(</?[bu]>)*(\w[\w.-]*)(</?[bu]>)*(\([0-9nlx]\w*\))"
   var paths: seq[string] = @[]
+  var ignoreMan = keyword.toUpperAscii()
+  if ignoreMan == keyword or keyword.len == 1:
+    ignoreMan = ""
   for p in getEnv("PATH").split(':'):
     var i = p.high
     while i > 0 and p[i] == '/':
@@ -212,6 +215,10 @@ proc processManpage(file: File; header: string) =
           secCap.s += offset
           secCap.e += offset
           let man = line[manCap.s..<manCap.e]
+          # ignore footers like MYPAGE(1)
+          # (just to be safe, we also check if it's in paths too)
+          if man == ignoreMan and not paths.isCommand(man):
+            continue
           let cat = man & line[secCap.s..<secCap.e]
           let link = "<a href='man:" & cat & "'>" & man & "</a>"
           line[manCap.s..<manCap.e] = link
@@ -233,7 +240,7 @@ proc doMan(man, keyword, section: string) =
   file.processManpage(header = """Content-Type: text/html
 
 <title>man """ & manword & """</title>
-<pre>""")
+<pre>""", keyword = keyword)
 
 proc doLocal(man, path: string) =
   # Note: we intentionally do not use -l, because it is not supported on
@@ -247,7 +254,7 @@ proc doLocal(man, path: string) =
   file.processManpage(header = """Content-Type: text/html
 
 <title>man -l """ & path & """</title>
-<pre>""")
+<pre>""", keyword = path.afterLast('/').until('.'))
 
 proc doKeyword(man, keyword, section: string) =
   let sectionOpt = if section == "": "" else: " -s " & section