def process_infile(indir, infile): """Process infile (.in) - substitutes and create reflection file (with the same name but without '.in' extension)""" f = change_ext(infile, '') shutil.copyfile(infile, f) buf = core.fread23(f) buf = substitute_infile(indir, buf) core.fwrite23(f, buf)
def onpost(self, parser=None, flush=None): if flush: jpath = self.jpath() chunk = parser.chunkdict.getbykey(self) if not parser.expand(jpath): raise core.ParserError("'%s' can not be expanded"%jpath) # body of out is output file name self.outfile = ''.join(self.body) self.outfile = os.path.join(parser.outdir, self.outfile) self.outfile = os.path.abspath(self.outfile) # if self.outfile has relative path, real outdir can be different then # in parser. If not exists, create it outdir = os.path.dirname(self.outfile) if not os.path.exists(outdir): os.makedirs(outdir) core.prn("writing to '%s'..."%self.outfile, engine=parser.engine, file='stdout') core.fwrite23(self.outfile, chunk.tangle) return core.Cmd.onpost(self, parser=parser, flush=flush)
def save(self): cssfname = os.path.join(self.parser.outdir, self.CSSFILENAME) if not os.path.exists(cssfname): # if CSS file doesnt exists, create core.prn("writing CSS-styles file '%s'..." % cssfname, engine=self.parser.engine, file='stdout') shutil.copyfile(core.extrapath(self.CSSFILENAME), cssfname) #core.fwrite23(cssfname, self._css) #if not self.parser.infile: # define output file name #fname = 'nanolp-refs.html' #else: #fname = os.path.split(self.parser.infile)[1] + '-refs.html' fname = self.inputattrs['name'] + '-refs.html' fname = os.path.join(self.parser.outdir, fname) lines = itertools.chain(self._header(), self._body(), self._footer()) text = '\n'.join(lines) core.prn("writing references file '%s'..." % fname, engine=self.parser.engine, file='stdout') core.fwrite23(fname, text)
def publish(self, filename): # modify input HTML... tokstream = core.HTMLTokensStream() tokstream.feed(core.fread23(filename)) headpos = 0 for itok, tok in enumerate(tokstream.tokens): if tok.match( tag='script', kind=core.HTMLToken.OPEN, attrs={'src': '/.*?%s.*?/' % re.escape(self.JSFILENAME)}): raise RuntimeError('This document is already modified') if tok.match(tag='head', kind=core.HTMLToken.CLOSE): headpos = itok break # Script 0 cmds = collections.OrderedDict() for cmd in self.parser.chunkdict.keys(): infile = cmd.srcinfo.infile if infile: infile = "" if infile == self.parser.infile else self.__url( infile) cmds[cmd.jpath()] = infile fmtargs = { 'SURR': self.parser.surr, 'CMDS': cmds, } for k, v in fmtargs.items(): fmtargs[k] = json.dumps(v).replace('"', "'") # ' instead of " text = self._script0.format(**fmtargs) script0 = [ core.HTMLToken(tag='script', kind=core.HTMLToken.OPEN, attrs={'type': 'text/javascript'}, data=text), core.HTMLToken(tag='script', kind=core.HTMLToken.CLOSE) ] # Script 1 script1 = [ core.HTMLToken(tag='script', kind=core.HTMLToken.OPEN, attrs='src=%s|type=text/javascript' % self.__url(self.JSFILENAME)), core.HTMLToken(tag='script', kind=core.HTMLToken.CLOSE) ] # Stylesheet css = [ core.HTMLToken(tag='link', kind=core.HTMLToken.SINGLE, attrs='href=%s|rel=stylesheet|type=text/css' % self.__url(self.CSSFILENAME)) ] # Head inserts tokstream.tokens[headpos:headpos] = css + script0 + script1 # Save to file xml = ''.join(tokstream.serialize()) core.prn("modifying '%s' for publish..." % filename, engine=self.parser.engine, file='stdout') core.fwrite23(filename, xml) # Flush additional files indir = os.path.dirname(filename) cssfname = os.path.join(indir, self.CSSFILENAME) jsfname = os.path.join(indir, self.JSFILENAME) if not os.path.exists(cssfname): core.prn("writing CSS-styles file '%s'..." % cssfname, engine=self.parser.engine, file='stdout') shutil.copyfile(core.extrapath(self.CSSFILENAME), cssfname) #core.fwrite23(cssfname, self._css) if not os.path.exists(jsfname): core.prn("writing JS file '%s'..." % jsfname, engine=self.parser.engine, file='stdout') shutil.copyfile(core.extrapath(self.JSFILENAME), jsfname)