21700: Install Bundler system-wide in Rails postinst
[arvados.git] / sdk / ruby-google-api-client / yard / lib / yard / serializers / wiki_serializer.rb
1 # encoding: utf-8
2
3 require 'yard/serializers/file_system_serializer'
4
5 module YARD
6   module Serializers
7     ##
8     # Subclass required to get correct filename for the top level namespace.
9     # :-(
10     class WikiSerializer < FileSystemSerializer
11       # Post-process the data before serializing.
12       # Strip unnecessary whitespace.
13       # Convert stuff into more wiki-friendly stuff.
14       # FULL OF HACKS!
15       def serialize(object, data)
16         data = data.encode("UTF-8")
17         if object == "Sidebar.wiki"
18           data = data.gsub(/^#sidebar Sidebar\n/, "")
19         end
20         data = data.gsub(/\n\s*\n/, "\n")
21         # ASCII/UTF-8 erb error work-around.
22         data = data.gsub(/--/, "—")
23         data = data.gsub(/——/, "----")
24         data = data.gsub(/----\n----/, "----")
25         # HACK! Google Code Wiki treats <code> blocks like <pre> blocks.
26         data = data.gsub(/\<code\>(.+)\<\/code\>/, "`\\1`")
27         super(object, data)
28       end
29
30       def serialized_path(object)
31         return object if object.is_a?(String)
32
33         if object.is_a?(CodeObjects::ExtraFileObject)
34           fspath = ['file.' + object.name + (extension.empty? ? '' : ".#{extension}")]
35         else
36           # This line is the only change of significance.
37           # Changed from 'top-level-namespace' to 'TopLevelNamespace' to
38           # conform to wiki word page naming convention.
39           objname = object != YARD::Registry.root ? object.name.to_s : "TopLevelNamespace"
40           objname += '_' + object.scope.to_s[0,1] if object.is_a?(CodeObjects::MethodObject)
41           fspath = [objname + (extension.empty? ? '' : ".#{extension}")]
42           if object.namespace && object.namespace.path != ""
43             fspath.unshift(*object.namespace.path.split(CodeObjects::NSEP))
44           end
45         end
46
47         # Don't change the filenames, it just makes it more complicated
48         # to figure out the original name.
49         #fspath.map! do |p|
50         #  p.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
51         #end
52
53         # Remove special chars from filenames.
54         # Windows disallows \ / : * ? " < > | but we will just remove any
55         # non alphanumeric (plus period, underscore and dash).
56         fspath.map! do |p|
57           p.gsub(/[^\w\.-]/) do |x|
58             encoded = '_'
59
60             x.each_byte { |b| encoded << ("%X" % b) }
61             encoded
62           end
63         end
64         fspath.join("")
65       end
66     end
67   end
68 end