| #!/usr/bin/env ruby |
| |
| |
| |
|
|
| |
| def markdown(s) |
| s = s.gsub(/\*\/$/,"") |
| s = s.gsub(/^ ?\* ?/,"") |
| s = s.gsub(/^\/\*\*? ?/,"") |
| s.chop! while s[-1] == "\n" || s[-1] == " " |
| lines = s.split("\n") |
| newlines = [] |
| |
| lines.each{|l| |
| |
| l = l.gsub(/(?<![A-Z_])RM_(?=[A-Z])/, 'RedisModule_') |
| |
| |
| if not l.start_with?(' ') |
| |
| l = l.gsub(/(?<!`)RedisModule[A-z]+(?:\*?\(\))?/){|x| "`#{x}`"} |
| |
| l = l.gsub(/(?<![`A-z.])[a-z_]+\(\)/, '`\0`') |
| |
| l = l.gsub(/(?<![`A-z\*])[A-Za-z]+_[A-Za-z0-9_]+/){|x| "`#{x}`"} |
| |
| l = l.gsub(/(^| )(https?:\/\/[A-Za-z0-9_\/\.\-]+[A-Za-z0-9\/])/, |
| '\1[\2](\2)') |
| |
| l = l.gsub(/ -- /, ' – ') |
| end |
| |
| l = l.gsub(/`(RedisModule_[A-z0-9]+)[()]*`/) {|x| |
| $index[$1] ? "[#{x}](\##{$1})" : x |
| } |
| newlines << l |
| } |
| return newlines.join("\n") |
| end |
|
|
| |
| |
| |
| def linebreak_proto(proto, indent) |
| if proto.bytesize <= 80 |
| return proto |
| end |
| parts = proto.split(/,\s*/); |
| if parts.length == 1 |
| return proto; |
| end |
| align_pos = proto.index("(") + 1; |
| align = " " * align_pos |
| result = parts.shift; |
| bracket_balance = 0; |
| parts.each{|part| |
| if bracket_balance == 0 |
| result += ",\n" + indent + align |
| else |
| result += ", " |
| end |
| result += part |
| bracket_balance += part.count("(") - part.count(")") |
| } |
| return result; |
| end |
|
|
| |
| |
| def docufy(src,i) |
| m = /RM_[A-z0-9]+/.match(src[i]) |
| name = m[0] |
| name = name.sub("RM_","RedisModule_") |
| proto = src[i].sub("{","").strip+";\n" |
| proto = proto.sub("RM_","RedisModule_") |
| proto = linebreak_proto(proto, " "); |
| |
| |
| puts "<span id=\"#{name}\"></span>\n\n" |
| puts "### `#{name}`\n\n" |
| puts " #{proto}\n" |
| puts "**Available since:** #{$since[name] or "unreleased"}\n\n" |
| comment = "" |
| while true |
| i = i-1 |
| comment = src[i]+comment |
| break if src[i] =~ /\/\*/ |
| end |
| comment = markdown(comment) |
| puts comment+"\n\n" |
| end |
|
|
| |
| def section_doc(src, i) |
| name = get_section_heading(src, i) |
| comment = "<span id=\"#{section_name_to_id(name)}\"></span>\n\n" |
| while true |
| |
| comment = comment + src[i] if src[i] !~ /^[\/ ]?\*{1,2} ?-{50,}/ |
| break if src[i] =~ /\*\// |
| i = i+1 |
| end |
| comment = markdown(comment) |
| puts comment+"\n\n" |
| end |
|
|
| |
| def section_name_to_id(name) |
| return "section-" + |
| name.strip.downcase.gsub(/[^a-z0-9]+/, '-').gsub(/^-+|-+$/, '') |
| end |
|
|
| |
| |
| def get_section_heading(src, i) |
| if src[i] =~ /^\/\*\*? \#+ *(.*)/ |
| heading = $1 |
| elsif src[i+1] =~ /^ ?\* \#+ *(.*)/ |
| heading = $1 |
| end |
| return heading.gsub(' -- ', ' – ') |
| end |
|
|
| |
| |
| |
| def is_section_doc(src, i) |
| return src[i] =~ /^\/\*\*? \#/ || |
| (src[i] =~ /^\/\*/ && src[i+1] =~ /^ ?\* \#/) |
| end |
|
|
| def is_func_line(src, i) |
| line = src[i] |
| return line =~ /RM_/ && |
| line[0] != ' ' && line[0] != '#' && line[0] != '/' && |
| src[i-1] =~ /\*\// |
| end |
|
|
| puts "---\n" |
| puts "title: \"Modules API reference\"\n" |
| puts "linkTitle: \"API reference\"\n" |
| puts "weight: 1\n" |
| puts "description: >\n" |
| puts " Reference for the Redis Modules API\n" |
| puts "aliases:\n" |
| puts " - /topics/modules-api-ref\n" |
| puts "---\n" |
| puts "\n" |
| puts "<!-- This file is generated from module.c using\n" |
| puts " utils/generate-module-api-doc.rb -->\n\n" |
| src = File.open(File.dirname(__FILE__) ++ "/../src/module.c").to_a |
|
|
| |
| $index = {} |
| src.each_with_index do |line,i| |
| if is_func_line(src, i) |
| line =~ /RM_([A-z0-9]+)/ |
| name = "RedisModule_#{$1}" |
| $index[name] = true |
| end |
| end |
|
|
| |
| $since = {} |
| git_dir = File.dirname(__FILE__) ++ "/../.git" |
| if File.directory?(git_dir) && `which git` != "" |
| `git --git-dir="#{git_dir}" tag --sort=v:refname`.each_line do |version| |
| next if version !~ /^(\d+)\.\d+\.\d+?$/ || $1.to_i < 4 |
| version.chomp! |
| `git --git-dir="#{git_dir}" cat-file blob "#{version}:src/module.c"`.each_line do |line| |
| if line =~ /^\w.*[ \*]RM_([A-z0-9]+)/ |
| name = "RedisModule_#{$1}" |
| if ! $since[name] |
| $since[name] = version |
| end |
| end |
| end |
| end |
| end |
|
|
| |
| puts "## Sections\n\n" |
| src.each_with_index do |_line,i| |
| if is_section_doc(src, i) |
| name = get_section_heading(src, i) |
| puts "* [#{name}](\##{section_name_to_id(name)})\n" |
| end |
| end |
| puts "* [Function index](#section-function-index)\n\n" |
|
|
| |
| src.each_with_index do |_line,i| |
| if is_func_line(src, i) |
| docufy(src, i) |
| elsif is_section_doc(src, i) |
| section_doc(src, i) |
| end |
| end |
|
|
| |
| puts "<span id=\"section-function-index\"></span>\n\n" |
| puts "## Function index\n\n" |
| $index.keys.sort.each{|x| puts "* [`#{x}`](\##{x})\n"} |
| puts "\n" |
|
|