Skip to content

Instantly share code, notes, and snippets.

@glurp
Last active December 20, 2018 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glurp/2a5eb71a4e1baf708c1b34034ba181bf to your computer and use it in GitHub Desktop.
Save glurp/2a5eb71a4e1baf708c1b34034ba181bf to your computer and use it in GitHub Desktop.
generation of sheat sheet documentation of ruby code
* {
font-size: small;
}
div#cols {
column-width: 20em;
column-gap: 4px;
column-rule-style: solid;
column-rule-color: #044;
}
h2,h3,h4,h5 {
background: linear-gradient(to right, #005070 0%,#007090 100%);
border-radius: 22px 22px;
box-shadow: 2px 2px 6px 1px #000;
margin-left: 30px;
padding-left: 30px;
}
h4,h5 {
background: linear-gradient(to right, #606060 0%,#808080 100%);
border-radius: 7px 7px;
box-shadow: 2px 2px 6px 1px #000;
margin-left: 3px;
padding-left: 30px;
}
code.c { background: #EEE}
code.r { background: #AEE}
code { padding-left: 10px }
require 'time'
require 'slop'
#############################################################"""
class T
def initialize(filename,css)
@name=File.basename(filename.split(/\./,2).first)
@css=css
@fout="#{@name}.html"
@out=[]
end
def template()
@tpl||= DATA.read
end
def caption(line)
header,caption=line.split(/\s/,2)
level=header.size+1
@out << "<h#{level}>#{pre(caption.strip)}</h#{level}>"
end
def comment(line)
bidon,comment=line.split(/\s/,2)
@out << "<p><b>#{pre(comment.strip)}</b></p>"
end
def process(col)
end
def process(page)
end
def get_binding() $binding end
def evaluate(line)
ev=eval(line,get_binding).inspect
lb= ev.size>30 ? "<br>&nbsp;&nbsp;&nbsp;" : ""
@out << "<code class='c'>#{pre(line)} </code>=>#{lb} <code class='r'>#{pre(ev)}</code><br/>"
#puts "Eval of #{line} \t: #{ev}"
rescue Exception => e
puts "ERROR for #{line} : #{e}"
@out << "<code class='c'>#{pre(line)} </code>=> <b>ERROR : #{e}</b><br>"
end
def pre(str)
str.gsub("&","&amp;").gsub("<","&lt;").gsub(">","&gt;")
end
def generate()
t=template().
gsub("%TTITLE%",@name.gsub(/[\W_]/," ")).
gsub("%CONTENT%",@out.join("\n")).
gsub("%DATE%",Time.now.to_datetime.rfc3339().gsub("T"," ")).
gsub("%CSS%", @css!="" ? "<style>\n#{File.read(@css)}\n</style>" : "")
File.write(@fout,t)
system("start",@fout)
end
end
$binding=binding
#############################################################"""
## M A I N
#############################################################"""
opt=Slop.parse do |o|
o.on "-v", "--version" do
puts "#{$0} v0.1"
exit(0)
end
o.on '--help' do
puts o
exit
end
o.string "-s", "--css","css file for render your sheat cheet", default: ""
end
filename=opt.args.last
css=opt[:css]
if !filename
puts opt
exit(0)
end
unless File.readable?(filename)
puts "File #{filename} do not exists !"
exit(1)
end
unless css=="" || File.readable?(css)
puts "File #{filename} do not exists !"
exit(1)
end
puts "Process #{filename}..."
traitment=T.new(filename,css)
File.read(filename).lines.each do |line| line.strip!
next if line.size<1
case line
when /^#/ then traitment.caption(line)
when /^>/ then traitment.comment(line)
when /%col/ then traitment.process(:col)
when /%page/ then traitment.process(:page)
else
traitment.evaluate(line)
end
end
traitment.generate()
__END__
<html>
<head>
<title>%TTITLE%</title>
<style>
body { margin: 0px; padding: 0px; }
h1 {text-align: center;}
h1,h2,h3,h4,h5,h6 { background: #034 ; color: white; padding-left: 3px;}
code.c { background: #EEE}
code.r { background: #AEE}
code { padding-left: 10px }
</style>
%CSS%
</head>
<body>
<h1>%TTITLE%</h1>
<div id='cols'>
%CONTENT%
</div>
<br>
<hr>
<center>Generated at %DATE%</center>
</body>
</html>
> Hello, this is test of cheat-sheet ruby generator.
# string to array....
"a,b,c,d".split(/,/)
"a,bb,c".scan(/\w+/)
# Array to string ....
[1,2,3].inspect
[1,2,3].join(",")
# Array to hash ....
Hash[1,2,3,4]
hdd = [1,2,3,4,5,6,7,8].group_by {|v| v%4}
# Hash to array....
> direct conversion !
hdd.to_a()
hdd.keys()
hdd.values()
#### Test special html char
> This comment has less-than, greater-than, ampersand : < ; >
> Warning! global var/ressources can be embedded in the docs!
ARGV.join(",")
@glurp
Copy link
Author

glurp commented Dec 20, 2018

result

Usage:

ruby shishi-generator.rb -s gen-style.css test.snip.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment