1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
#!/usr/bin/env ruby
#
# Internal helper script to create a markdown file from binaries
#
# The --index option creates the index page
#
# bin2md [--index] erbtemplate [binary]
#
# by Pjotr Prins (C) 2020
#
# The rules are simple USAGE:
#
# usage: can be multiline block anywhere in the output
#
# After removing that the next block is the DESCRIPTION.
# Onle line create a TYPE (see TYPES).
# The rest are the OPTIONS.
require 'erb'
require 'date'
require 'open3'
# cerr << endl << "Type: statistics" << endl << endl;
# cerr << endl << "Type: transformation" << endl << endl;
TYPES = ["filter","metrics","phenotype","genotype","transformation","statistics"]
=begin
if (argc == 2) {
string h_flag = argv[1];
if (h_flag == "-h" || h_flag == "--help") {
cerr << R"(
Generate a random VCF file
Usage: vcfrandom
Example:
vcfrandom
Type: statistics
)";
exit(1);
}
}
=end
VERSION=`cat ./VERSION`.strip
template = ARGV.shift
is_man = false # creating man pages?
if template == "--man"
is_man = true
template = ARGV.shift
end
create_index = false
if template == "--index"
create_index = true
template = ARGV.shift
index = []
end
search = ARGV.shift
bindir = './build'
$stderr.print("--- Parsing the bin files in #{bindir} for #{VERSION}\n")
d = DateTime.now
year = d.year
Dir.glob(bindir+'/*').each do|bin|
if !File.directory?(bin) and File.executable?(bin)
if search and bin !~ /#{search}/
next
end
cmd = File.basename(bin)
help_cmd = cmd + " -h"
$stderr.print(" "+bin+"\n")
stdout, stderr, status = Open3.capture3(bin+" -h")
out = stderr + stdout
if out == ""
help_cmd = cmd
stdout, stderr, status = Open3.capture3(bin)
out = stderr + stdout
end
# out = ,:encoding => 'UTF-8'
out = out.encode('UTF-8')
# $stderr.print(out)
lines = (out).split("\n")
lines = lines.map{|l| l.gsub(/#{Regexp.escape(cmd)}/,"**#{cmd}**")}
lines = lines.map{|l| l.gsub(/\.+\/build\//,"")}
lines = lines.map{|l| l.gsub(/INFO: help:?/,"")}
lines = lines.map{|l| l.gsub(/INFO: description:/,"")}
lines = lines.map{|l| l.gsub(/INFO:\s+/,"")}
pydoc_full = lines.map{|l| l=="" ? '>' : l }.join("\n")
in_usage = false
has_options = nil
has_example = false
usage = []
other = []
example = []
lines.shift while lines[0] == ""
lines.each do | l |
break if l == "------------------------------------------------------"
if l =~ /usage/i
in_usage = true
end
if in_usage
if l == "" or l =~ /^Output/i
in_usage = false
next
end
usage << l
else
if l =~ /^Example:/
has_example = true
end
if has_example
example << l
else
other << l
end
end
end
descr = []
rest = other
type = "unknown"
(other+example).each do | l |
if l =~ /type: (\S+)/i
type = $1
raise "Unknown type #{type} for #{cmd}" if !TYPES.include?(type)
break
end
end
other.each do | l |
break if l == "" or l =~ /^Output/i or l =~ /Options/i
descr << l
end
if descr == []
lineno = 0
rr = rest.reverse
rr.each_with_index do | l,i |
if l != "" and l !~ /^Type/i
lineno = i
break
end
end
rr = rr[lineno..-1]
rr.each do | l |
if descr.length and l == "" or l =~ /^\s/
descr = descr.reverse
break
end
descr << l
end
rest = rr.drop(descr.size).reverse
else
rest = rest.drop(descr.size)
end
body = rest.join("\n")
has_options = true if body != ""
usage = usage.join(" ").gsub(/#{VERSION}\s+/,"")
usage = usage.gsub(/usage:\s+/i,"")
usage = usage.gsub(/\s+/," ").strip
pydoc_full = pydoc_full.gsub(/#{VERSION}\s+/,"")
pydoc_full = pydoc_full.gsub(/\.\.\/build\//,"")
# pydoc_full = pydoc_full.gsub(/vcflib/,"VCF")
descr = descr.join(" ").gsub(/#{VERSION}\s+/,"")
descr = descr.sub(/vcflib/,"VCF")
descr = descr.gsub(/\s+/," ").strip
example = example.join("\n")
# print("HELP:",help_cmd,"\n")
# print("DESCRIPTION:",descr,"\n")
# print("USAGE:",usage,"\n")
# print("TYPE:",type,"\n")
# print("BODY:",body,"\n")
if create_index
rec = {
cmd: cmd,
type: type,
descr: descr
}
index << rec
else
b = binding
renderer = ERB.new(File.read(template))
File.open("./doc/#{cmd}.md","w") { |f|
f.print renderer.result(b)
}
end
end
end
if create_index
require 'ostruct'
renderer = ERB.new(File.read("./test/scripts/index-item.erb"))
File.open("./doc/vcflib.md","w") { |f|
f.print <<HEADER
% vcflib(1) vcflib | vcflib (index)
% Erik Garrison and vcflib contributors
# NAME
**vcflib** index
# DESCRIPTION
vcflib contains tools and libraries for dealing with the Variant Call
Format (VCF) which is a flat-file, tab-delimited textual format
intended to describe reference-indexed variations between
individuals.
VCF provides a common interchange format for the description of
variation in individuals and populations of samples, and has become
the defacto standard reporting format for a wide array of genomic
variant detectors.
vcflib provides methods to manipulate and interpret sequence variation
as it can be described by VCF. It is both:
* an API for parsing and operating on records of genomic variation as it can be described by the VCF format,
* and a collection of command-line utilities for executing complex manipulations on VCF files.
The API itself provides a quick and extremely permissive method to
read and write VCF files. Extensions and applications of the library
provided in the included utilities (*.cpp) comprise the vast bulk of
the library's utility for most users.
<!--
Created with ./scripts/bin2md.rb --index
-->
HEADER
TYPES.each do | type |
f.print %{
## #{type}
| #{type} command | description |
| :-------------- | :---------- |
}
index.each do | rec |
rec = OpenStruct.new(rec)
if rec.type == type
b = binding
f.print renderer.result(b)
end
end
end
github = "https://github.com/vcflib/vcflib"
f.print <<FOOTER
# SOURCE CODE
See the source code repository at #{github}
# LICENSE
Copyright 2011-#{year} (C) Erik Garrison and vcflib contributors. MIT licensed.
FOOTER
}
end
|