def include(file, basename="./", mode="none"): # regex filter to find out include statement _include = re.compile("`([^`]+)`\{.include}") # regex filter to find out rotate statement _rotimg = re.compile( "`([^`]+)`\{.rotate\ +(\.caption\ *=\ *[^`\.]+)\ +(\.angle\ *=\ *[^`\.]+)}") # regex filter to find out listing statement listing = re.compile("`([^`]+)`\{.listingtable\ (\.[^`\.]+)}") # regex filter to remove markdown comment stripped = re.sub("<!--[\s\S]*?-->", "", open(file, "rb").read()) _mode = modesel[mode.upper()] # print _mode output = [] for line in stripped.split("\n"): if _include.search(line): print file + ": include", input_file = _include.search(line).groups()[0] print input_file # file_contents = open(input_file, "rb").read() line = _include.sub(line, include(basename + input_file, mode=mode)) if listing.search(line): print file + ": listingtable of", input_file = listing.search(line).groups()[0] filetype = listing.search(line).groups()[1] print filetype convert = f2l(input_file, filetype, _mode[0], _mode[1]) line = listing.sub(line, convert) if _rotimg.search(line): print file + ": rotate image of", input_file = _rotimg.search(line).groups()[0] caption = _rotimg.search(line).groups()[1] angle = _rotimg.search(line).groups()[2] print input_file, angle rotatedcaption = rotatepic(input_file, caption, angle) # print rotatedcaption line = listing.sub(line, rotatedcaption) output.append(line) return "\n".join(output)
for line in stripped.split("\n"): if _include.search(line): print "main: include", input_file = _include.search(line).groups()[0] print input_file line = _include.sub(line, include(input_file, _basedir, _mode)) if listing.search(line): print "main: listingtable of", input_file = listing.search(line).groups()[0] filetype = listing.search(line).groups()[1] print filetype convert = f2l(input_file, filetype, __mode[0], __mode[1]) line = listing.sub(line, convert) if _rotimg.search(line): print "main: rotate image of", input_file = _rotimg.search(line).groups()[0] caption = _rotimg.search(line).groups()[1] angle = _rotimg.search(line).groups()[2] print input_file, angle rotatedcaption = rotatepic(input_file, caption, angle) # print rotatedcaption line = listing.sub(line, rotatedcaption) if tblcaption.search(line): if(__mode[0]): caption = tblcaption.search(line).groups()[2] link = tblcaption.search(line).groups()[4] line = "TC \"[@" + link + "] " + caption + "\" `\l` 5\n\n" + line print link output.write(line + "\n")