Skip to content

Commit

Permalink
Merge pull request #1 from ljs015/master
Browse files Browse the repository at this point in the history
minor bug fixes & another handler based off of your basic.awk (ljs015)
  • Loading branch information
Andrés J. Díaz committed Oct 11, 2012
2 parents 99a2ca5 + a108200 commit 3b33615
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 5 deletions.
9 changes: 5 additions & 4 deletions bashdoc
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ fi

# Determine in .py extension is present in rst2* scripts. Debian and other
# deb-based distributions omit the extension of the scripts.
require rst2${format} || { require rst2${format}.py && py=".py" ;} ||
E=1 fatal $"rst backend not found (rst2${format})"
case $format in
none|raw|rst) ;;
*) require rst2${format} || { require rst2${format}.py && py=".py" ;} || E=1 fatal $"rst backend not found (rst2${format})" ;;
esac

# Redirect output to output_file if present.
if [ "${output_file}" ]; then
Expand All @@ -102,9 +104,8 @@ fi

# Handler for none format (aka raw, aka rst). If none format is used, then
# result is in plain rst format.
case $2 in
case $format in
none|raw|rst) rst_maker "$1";;
*) rst_maker "$1" | rst2${format}${py} ;;
esac

#- vim:foldmethod=syntax:foldenable:
2 changes: 1 addition & 1 deletion lib/basic.awk
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ BEGIN { last_comment=0; }
# Code block parser. We consider a code block everthing which are not
# a comment block. We print the :: mark (starts a pre-formated block in RST)
# only at the beggining of code block.
/^[:space:]*[^#\n].*/ {
/^[[:space:]]*[^#\n].*/ {
if (last_comment == 1)
printf "\n\n::\n\n"
last_comment=0
Expand Down
84 changes: 84 additions & 0 deletions lib/basic.debug.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# This is a basic gawk backend for bashdoc(1)
# (c) 2007 Andrés J. Díaz <[email protected]>
# --
# This file is part of bashdoc project.

# At the beggining set the flag last_comment to 0, this flag is used to
# detect when a code block starts (or a comment block ends).
BEGIN { last_comment=0; debug=1 }

# We ignore lines which starts by a comment followed by exclamation mark or
# minus sign.
/^[#]!/ {
if (debug == 0)
next
else
print "<ignore>"$0
}
/^[#]-/ {
if (debug == 0)
next
else
print "<ignore>"$0
}

# Code block parser. We consider a code block everthing which are not
# a comment block. We print the :: mark (starts a pre-formated block in RST)
# only at the beggining of code block.
/^[:space:]*[^#\n].*/ {
if (last_comment == 1)
printf "\n\n::\n\n"
last_comment=0
if (debug == 0)
print " "$0
else
print " <code>"$0
}

# The following rules create a RST notes for each codification label, such
# as TODO, FIXME and XXX.
/^[#][ ]*TODO.*/ {
print "\n.. note:: To-Do"
sub("^[#][ ]*TODO[:]?","")
if (debug == 0)
print " "$0
else
print " <TODO>"$0
}
/^[#][ ]*FIXME.*/ {
print "\n.. note:: Fix me"
sub("^[#][ ]*FIXME[:]?","")
if (debug == 0)
print " "$0
else
print " <FIXME>"$0
}
/^[#][ ]*XXX.*/ {
print "\n.. note:: Hack"
sub("^[#][ ]*XXX[:]?","")
if (debug == 0)
print " "$0
else
print " <HACK>"$0
}

# Comment block parser. A comment block is a number of lines starting by
# a sharp symbol (#). We also accept lines in advanced bsah guide format,
# such as #+ and #>. Some text are parser specially, for example the
# copyright line in emphatized, and both, copyright and registered symbols
# are substituted.
/^[#].*/ {
last_comment=1
sub("^[:space:]*#[+> ]*","")
sub("^[Cc]opyright.*","*&*")
sub("\\([cC]\\)","©")
sub("\\([rR]\\)","®")

if (debug == 0)
printf "\n%s",$0
else
printf "\n<comment>%s",$0
}

# Finally print the EOL (we do not put the EOL when parsing comment block.
END { printf "\n" }
149 changes: 149 additions & 0 deletions lib/better.basic.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# This is an attempted at a correction to the basic gawk backend for bashdoc(1)
# the author doesn't really understand liscenses & copyright; don't be a dick.

# initial state
BEGIN {
debug = 0
bash_pound_comment_line = 0
styled_bash_pound_comment_line = 0
consecutive_noncomment_lines = 0
ignore_this_line = 0
}

# Skip shebangs
(FNR == 1) && /^#!.*$/ {
if (debug == 1)
print "*next*; shebang:"$0
next
}

# I haven't played around with these yet; straight from the original basic.awk:
# The following rules create a RST notes for each codification label, such
# as TODO, FIXME and XXX.
# /^[#][ ]*TODO.*/ {
# print "\n.. note:: To-Do"
# sub("^[#][ ]*TODO[:]?","")
# if (debug == 0)
# print " "$0
# else
# print " <TODO>"$0
# }
# /^[#][ ]*FIXME.*/ {
# print "\n.. note:: Fix me"
# sub("^[#][ ]*FIXME[:]?","")
# if (debug == 0)
# print " "$0
# else
# print " <FIXME>"$0
# }
# /^[#][ ]*XXX.*/ {
# print "\n.. note:: Hack"
# sub("^[#][ ]*XXX[:]?","")
# if (debug == 0)
# print " "$0
# else
# print " <HACK>"$0
# }

# detection first, printing later
# -------------------------------

# detect anything that is not a 'bash-#-comment-only line'
/^[[:space:]]*[^#].*$/ {
bash_pound_comment_line = 0
styled_bash_pound_comment_line = 0
ignore_this_line = 0
if (debug == 1)
print "not a 'bash-#-comment-only line':"$0
}
# detect anything that is a 'bash-#-comment-only line'
/^[[:space:]]*[#].*$/ {
bash_pound_comment_line = 1
styled_bash_pound_comment_line = 0
ignore_this_line = 0
if (debug == 1)
print "is a 'bash-#-comment-only line':"$0
}
# detect anything that is a styled 'bash-#-comment-only line' and which we should not ignore
/^[[:space:]]*[#][![:space:]].*$/ {
if ((debug == 1) && (bash_pound_comment_line != 1))
print "*error state*; 'bash-#-comment-only line' should have already registered."
styled_bash_pound_comment_line = 1
ignore_this_line = 0
if (debug == 1)
print "is a styled 'bash-#-comment-only line':"$0
}
# detect anything that is a styled 'bash-#-comment-only line' and which we should ignore
/^[[:space:]]*[#][^![:space:]].*$/ {
if ((debug == 1) && (bash_pound_comment_line != 1))
print "*error state*; 'bash-#-comment-only line' should have already registered."
styled_bash_pound_comment_line = 1
ignore_this_line = 1
if (debug == 1)
print "*ignored*; is a styled 'bash-#-comment-only line':"$0
}
# detect blank lines because nothing else will
/^$/ {
bash_pound_comment_line = 0
styled_bash_pound_comment_line = 0
ignore_this_line = 1
if (debug == 1)
print "*ignored*; is a blank line:"$0
}
# I can't think of a way to account for the following comments using a single regexp or just a few regexps (textbook regexs shouldn't be able to catch the patterns of these lines)
# These could be accounted for individually, but that would be bad for users (it would dash their unentitled expectations and make them rage, a lot) and would not scale to cover the more general problem.
#
# ^$((muffin--)) # I have less muffins now.$
#
# ^wigglytuff_used='#' # It's not very effective.$
#
# ^wigglytuff_used='##' # It's not very effective.$
#
# ^wigglytuff_used='#"' # It's not very effective.$
#
# ^wigglytuff_used='"#"' # It's not very effective.$
#
# ^wigglytuff_used="#'" # It's not very effective.$
#
# ^wigglytuff_used="# # It's not very effective.$
#
# ^} # And *that* was the best function ever.$
#
# ^{ # don't you want some chocolate right now?$
#
# ^{ # } # tread carefully if you try to implement single curly-brace cases$
#

# printing now
# ------------

# If the ignore_this_line flag's state survived this far, then honor it.
ignore_this_line == 1 {
if (debug == 1)
print "*next*; *ignored*:"$0
next
}

# print a thing that is exactly a 'bash-#-comment-only line'
(bash_pound_comment_line == 1) && (styled_bash_pound_comment_line == 0) && (parentheses_then_bash_pound_comment_line == 0) {
sub("^[[:space:]]*#","")
printf "\n%s",$0
consecutive_noncomment_lines = 0
}
# print a thing that is exactly a styled 'bash-#-comment-only line'
(bash_pound_comment_line == 1) && (styled_bash_pound_comment_line == 1) && (parentheses_then_bash_pound_comment_line == 0) {
sub("^[[:space:]]*#[![:space:]]","")
printf "\n%s",$0
consecutive_noncomment_lines = 0
}
# anything that is not a comment, is code; For each code line, if it is consecutively the first such of its code block, then treat it specially.
(bash_pound_comment_line == 0) && (consecutive_noncomment_lines == 0) && (parentheses_then_bash_pound_comment_line == 0) {
printf "\n\n::\n\n"
}
# anything that is not a comment, is code; print it now.
bash_pound_comment_line == 0 {
print " "$0
++consecutive_noncomment_lines
}

END { printf "\n" }

0 comments on commit 3b33615

Please sign in to comment.