nitdoc: escape html chars in documentation
[nit.git] / src / nitdoc.nit
index 104bfef..5543600 100644 (file)
@@ -243,11 +243,11 @@ class DocContext
        do
                var s = opt_source.value
                if s == null then
-                       add("in #{l.file.filename}")
+                       add("in #{l.file.filename.simplify_path}")
                else
                        # THIS IS JUST UGLY ! (but there is no replace yet)
                        var x = s.split_with("%f")
-                       s = x.join(l.file.filename)
+                       s = x.join(l.file.filename.simplify_path)
                        x = s.split_with("%l")
                        s = x.join(l.line_start.to_s)
                        x = s.split_with("%L")
@@ -298,6 +298,42 @@ class DocContext
        end
 end
 
+redef class String
+       # Replace all occurence of pattern ith string
+       fun replace(p: Pattern, string: String): String
+       do
+               return self.split_with(p).join(string)
+       end
+
+       # Escape the following characters < > & and " with their html counterpart
+       fun html_escape: String
+       do
+               var ret = self
+               if ret.has('&') then ret = ret.replace('&', "&amp;")
+               if ret.has('<') then ret = ret.replace('<', "&lt;")
+               if ret.has('>') then ret = ret.replace('>', "&gt;")
+               if ret.has('"') then ret = ret.replace('"', "&quot;")
+               return ret
+       end
+
+       # Remove "/./", "//" and "bla/../"
+       fun simplify_path: String
+       do
+               var a = self.split_with("/")
+               var a2 = new Array[String]
+               for x in a do
+                       if x == "." then continue
+                       if x == "" and not a2.is_empty then continue
+                       if x == ".." and not a2.is_empty then
+                               a2.pop
+                               continue
+                       end
+                       a2.push(x)
+               end
+               return a2.join("/")
+       end
+end
+
 # A virtual module is used to work as an implicit main module that combine unrelated modules
 # Since conflict may arrise in a virtual module (the main method for instance) conflicts are disabled
 class MMVirtualModule
@@ -1054,13 +1090,13 @@ redef class ADoc
                for c in n_comment do
                        res.append(c.text.substring_from(1))
                end
-               return res.to_s
+               return res.to_s.html_escape
        end
 
        # Oneliner transcription of the doc
        fun short: String
        do
-               return n_comment.first.text.substring_from(1)
+               return n_comment.first.text.substring_from(1).html_escape
        end
 end