def get_master_doc(path): global temp_files rstfiles = [f for f in os.listdir(path) if re.match('.*\.rst$',f)] if len(rstfiles) == 0: srcfiles = [f for f in os.listdir(path) if re.match('.*\.x?c$',f)] if len(srcfiles) == 1: fname = srcfiles[0] gendir = os.path.join(path,'_build','gen') if not os.path.exists(gendir): os.makedirs(gendir) destname = fname.replace('.xc','.rst').replace('.c','.rst') destname = os.path.join('_build','gen',destname) if fname == destname: print "ARRGH, dodgy source file" sys.exit(1) src_path = os.path.join(path,fname) dst_path = os.path.join(path,destname) dst = open(dst_path,"w") print "Processing %s to create document" % fname from unweave import unweave for line in unweave(open(src_path).readlines()): dst.write(line) dst.close() rstfiles = [destname] if 'index.rst' in rstfiles: return 'index' elif len(rstfiles) == 1: return rstfiles[0][:-4] else: sys.stderr.write("Cannot determine main rst file\n") sys.exit(1)
def do_code_unweave(app, docname, source): lines = source[0].split("\n") for i in range(0, len(lines)): line = lines[i] m = re.match(".. minihowto::\s*(.+)$", line) if not m: continue filename = m.groups(0)[0].strip() env = app.env docdir = path.dirname(env.doc2path(env.docname, base=None)) dirs = [docdir, os.path.join(docdir, "_build", ".sources")] if os.path.exists(os.path.join("_build", ".sources")): dirs += [ os.path.join(docdir, "_build", ".sources", x) for x in os.listdir(os.path.join("_build", ".sources")) ] fns = [path.join(x, filename) for x in dirs] encoding = env.config.source_encoding codec_info = codecs.lookup(encoding) srclines = None for fn in fns: if srclines == None: try: f = codecs.StreamReaderWriter(open(fn, "U"), codec_info[2], codec_info[3], "strict") srclines = f.readlines() f.close() except (IOError, OSError): pass except UnicodeError: return [ document.reporter.warning( "Encoding %r used for reading included file %r" "seems to " "be wrong, try giving an :encoding: option" % (encoding, filename) ) ] if srclines: captured = unweave(srclines) else: captured = ["Cannot find: ", filename] lines[i] = "".join(captured) source[0] = "\n".join(lines)