Skip to content

Instantly share code, notes, and snippets.

@iboB
Created June 21, 2016 21:00
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 iboB/925542e1ed5afee2b0083ff4fa970462 to your computer and use it in GitHub Desktop.
Save iboB/925542e1ed5afee2b0083ff4fa970462 to your computer and use it in GitHub Desktop.
# c2md - v1.0 - public domain
# authored in 2016 by Borislav Stanimirov
# Used to generate dox files from cpp files
if ARGV.length == 0 || ARGV[0] == '--help' || ARGV[0] == '-?'
puts 'c2md'
puts 'Generate markdown doc from a C-like language'
puts
puts 'Usage:'
puts 'ruby c2md.rb -i <source files> -t <templates>'
puts
exit(0)
end
if ARGV[0] == '--version' || ARGV[0] == '-v'
puts 'c2md version 1.0.0'
puts
exit(0)
end
if ARGV[0] != '-i'
puts 'Missing source files'
puts
exit(1)
end
inputs = []
ai = 1
1.upto(ARGV.length - 1) do |i|
ai += 1
break if ARGV[i] == '-t'
inputs << ARGV[i]
end
if inputs.empty?
puts 'Missing source files'
puts
exit(1)
end
templates = []
ai.upto(ARGV.length - 1) do |i|
templates << ARGV[i]
end
if templates.empty?
puts 'Missing templates'
puts
exit(1)
end
blocks = {}
inputs.each do |fname|
input = File.open(fname, 'r')
cur_block_name = ''
cur_block = []
is_code = false
is_text = false
input.each_line do |line|
if cur_block_name.empty?
cur_block_name = line[3..-2] if line.start_with?('//[')
else
if line.start_with?('//]')
cur_block << '```' if is_code
blocks[cur_block_name.to_sym] = cur_block
cur_block_name = ''
cur_block = []
is_code = false
is_text = false
elsif line.start_with?('/*`')
cur_block << '```' if is_code
is_code = false
is_text = true
elsif line.start_with?('*/')
is_text = false
elsif line.start_with?('//` ')
cur_block << '```' if is_code
is_code = false
cur_block << line[4..-2].rstrip
elsif line.start_with?('//`')
cur_block << '```' if is_code
is_code = false
cur_block << line[3..-2].rstrip
elsif is_text || is_code
cur_block << line.rstrip
else
if !line.strip.empty?
cur_block << '```'
cur_block << line.rstrip
is_code = true
end
end
end
end
end
text_blocks = {}
blocks.each do |k, v|
text_blocks[k] = v.join("\n")
end
expand_blocks = {}
text_blocks.each do |k, v|
expand_blocks[k] = v % text_blocks
end
templates.each do |tname|
fname = tname.chomp('.template')
template_src = File.open(tname, 'r').read
File.open(fname, 'w').write(template_src % expand_blocks)
end
$ ruby c2md.rb -i src1.cpp src2.cpp -t doc.md.template

Some markdown

Source 1

Stuff

Lorem ipsum dolor sit amet, clita utamur patrioque his at, ne mei prompta meliore. Nam summo tamquam ei, nostrum iudicabit cum eu, praesent dignissim ex cum. Eam probo scaevola in, ut nam affert qualisque. Mei ad ancillae senserit instructior.

  • Elem 1
  • Elem 2
  • Elem 3
class actual_code
{
    int foo;
    float bar;
};

Cu eruditi inermis partiendo mea, mel primis vocent abhorreant ne. Cu appareat conceptam nam, has ex omnes veritus phaedrum. Simul impedit liberavisse ea nam, quo imperdiet gubergren consectetuer te, alii dicam honestatis nam ne.

Source 2

Beer

Sumo phaedrum temporibus eu qui, eu has vide phaedrum. Audiam legendos an cum. Has elit exerci ut, in qui paulo invidunt reprehendunt.

Impetus fabellas cum ei, alia repudiare adolescens sit ex. Cum iriure tacimates et, offendit constituto ei vis, id vocent dolores probatus mel.

First Header Second Header
Content Cell Content Cell
Content Cell Content Cell
int foo()
{
    // why not Zoidberg?
}

void Bar()
{
}

Why not more text?

Why not more code?

ok();

Parting Words

Bye bye!

# Some markdown
## Source 1
Stuff
%{src1}
## Source 2
Beer
%{src2}
## Parting Words
Bye bye!
#include "stuff.h"
//[src1
/*`
Lorem ipsum dolor sit amet, clita utamur patrioque his at, ne mei prompta
meliore. Nam summo tamquam ei, nostrum iudicabit cum eu, praesent dignissim ex
cum. Eam probo scaevola in, ut nam affert qualisque. Mei ad ancillae senserit
instructior.
* Elem 1
* Elem 2
* Elem 3
*/
class actual_code
{
int foo;
float bar;
};
/*`
Cu eruditi inermis partiendo mea, mel *primis* vocent abhorreant ne. Cu appareat
conceptam nam, has ex omnes veritus phaedrum. Simul impedit liberavisse ea nam,
quo imperdiet gubergren consectetuer te, **alii** dicam honestatis nam ne.
*/
//]
void func_which_does_not_matter() {}
//[src2
/*`
Sumo phaedrum temporibus eu qui, eu has vide phaedrum. Audiam legendos an cum.
Has elit exerci ut, in qui paulo invidunt reprehendunt.
Impetus fabellas cum ei, alia repudiare adolescens sit ex. Cum iriure tacimates
et, offendit constituto ei vis, id vocent dolores probatus mel.
First Header | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Cell
*/
int foo()
{
// why not Zoidberg?
}
void Bar()
{
}
/*`
Why not more text?
Why not more code?
*/
ok();
//]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment