def textileProcess(self, ctx, content): """ An XSL-T extension function to process textile formatting included in a post. Note: this doesn't work correctly if you want to also post process the result as XSL. content - a single element list containing an xmlNode containing the data to style """ try: node = libxml2.xmlNode(_obj=content[0]) parserContext = libxslt.xpathParserContext(_obj=ctx) xpathContext = parserContext.context() resultContext = xpathContext.transformContext() # If this is CDATA content we want textile to treat like a pre tag content = replace(node.serialize(), "<![CDATA[", "<pre>") content = replace(content, "]]>", "</pre>") source = textile.textile(content) source = replace(source, "<pre>", "") source = replace(source, "</pre>", "") return source except: sys.stderr.write("Textile processing error ") sys.stderr.write(source) return ""
def translate(ctx, str, translation_table, emphasis=None): global nodeName try: pctxt = libxslt.xpathParserContext(_obj=ctx) ctxt = pctxt.context() tctxt = ctxt.transformContext() nodeName = tctxt.insertNode().name except: pass typeform = len(str)*[emphasisMap[emphasis]] if emphasis else None braille = louis.translate([translation_table], str.decode('utf-8'), typeform=typeform)[0] return braille.encode('utf-8')
def f(ctx, str): global nodeName # # Small check to verify the context is correcly accessed # try: pctxt = libxslt.xpathParserContext(_obj=ctx) ctxt = pctxt.context() tctxt = ctxt.transformContext() nodeName = tctxt.insertNode().name except: pass return string.upper(str)
def translate(ctx, str, translation_table, emphasis=None): global nodeName try: pctxt = libxslt.xpathParserContext(_obj=ctx) ctxt = pctxt.context() tctxt = ctxt.transformContext() nodeName = tctxt.insertNode().name except: pass typeform = len(str) * [emphasisMap[emphasis]] if emphasis else None braille = louis.translate([translation_table], str.decode('utf-8'), typeform=typeform)[0] return braille.encode('utf-8')
def adjustColumnWidths(ctx, nodeset): # # Small check to verify the context is correcly accessed # try: pctxt = libxslt.xpathParserContext(_obj=ctx) ctxt = pctxt.context() tctxt = ctxt.transformContext() except: pass # Get the nominal table width varString = lookupVariable(tctxt, "nominal.table.width", None) if varString == None: nominalWidth = 6 * pixelsPerInch; else: nominalWidth = convertLength(varString); # Get the requested table width tableWidth = lookupVariable(tctxt, "table.width", "100%") foStylesheet = (tctxt.variableLookup("stylesheet.result.type", None) == "fo") relTotal = 0 relParts = [] absTotal = 0 absParts = [] colgroup = libxml2.xmlNode(_obj = nodeset[0]) # If this is an foStylesheet, we've been passed a list of fo:table-columns. # Otherwise we've been passed a colgroup that contains a list of cols. if foStylesheet: colChildren = colgroup else: colChildren = colgroup.children col = colChildren while col != None: if foStylesheet: width = col.prop("column-width") else: width = col.prop("width") if width == None: width = "1*" relPart = 0.0 absPart = 0.0 starPos = string.find(width, "*") if starPos >= 0: relPart, absPart = string.split(width, "*", 2) relPart = float(relPart) relTotal = relTotal + float(relPart) else: absPart = width pixels = convertLength(absPart) absTotal = absTotal + pixels relParts.append(relPart) absParts.append(pixels) col = col.next # Ok, now we have the relative widths and absolute widths in # two parallel arrays. # # - If there are no relative widths, output the absolute widths # - If there are no absolute widths, output the relative widths # - If there are a mixture of relative and absolute widths, # - If the table width is absolute, turn these all into absolute # widths. # - If the table width is relative, turn these all into absolute # widths in the nominalWidth and then turn them back into # percentages. widths = [] if relTotal == 0: for absPart in absParts: if foStylesheet: inches = absPart / pixelsPerInch widths.append("%4.2fin" % inches) else: widths.append("%d" % absPart) elif absTotal == 0: for relPart in relParts: rel = relPart / relTotal * 100 widths.append(rel) widths = correctRoundingError(widths) else: pixelWidth = nominalWidth if string.find(tableWidth, "%") < 0: pixelWidth = convertLength(tableWidth) if pixelWidth <= absTotal: print "Table is wider than table width" else: pixelWidth = pixelWidth - absTotal absTotal = 0 for count in range(len(relParts)): rel = relParts[count] / relTotal * pixelWidth relParts[count] = rel + absParts[count] absTotal = absTotal + rel + absParts[count] if string.find(tableWidth, "%") < 0: for count in range(len(relParts)): if foStylesheet: pixels = relParts[count] inches = pixels / pixelsPerInch widths.append("%4.2fin" % inches) else: widths.append(relParts[count]) else: for count in range(len(relParts)): rel = relParts[count] / absTotal * 100 widths.append(rel) widths = correctRoundingError(widths) # Danger, Will Robinson! In-place modification of the result tree! # Side-effect free? We don' need no steenkin' side-effect free! count = 0 col = colChildren while col != None: if foStylesheet: col.setProp("column-width", widths[count]) else: col.setProp("width", widths[count]) count = count+1 col = col.next return nodeset
def adjustColumnWidths(ctx, nodeset): # # Small check to verify the context is correcly accessed # try: pctxt = libxslt.xpathParserContext(_obj=ctx) ctxt = pctxt.context() tctxt = ctxt.transformContext() except: pass # Get the nominal table width varString = lookupVariable(tctxt, "nominal.table.width", None) if varString == None: nominalWidth = 6 * pixelsPerInch; else: nominalWidth = convertLength(varString); # Get the requested table width tableWidth = lookupVariable(tctxt, "table.width", "100%") foStylesheet = (tctxt.variableLookup("stylesheet.result.type", None) == "fo") relTotal = 0 relParts = [] absTotal = 0 absParts = [] colgroup = libxml2.xmlNode(_obj = nodeset[0]) # If this is an foStylesheet, we've been passed a list of fo:table-columns. # Otherwise we've been passed a colgroup that contains a list of cols. if foStylesheet: colChildren = colgroup else: colChildren = colgroup.children col = colChildren while col != None: if foStylesheet: width = col.prop("column-width") else: width = col.prop("width") if width == None: width = "1*" relPart = 0.0 absPart = 0.0 starPos = string.find(width, "*") if starPos >= 0: relPart, absPart = string.split(width, "*", 2) relPart = float(relPart) relTotal = relTotal + float(relPart) else: absPart = width pixels = convertLength(absPart) absTotal = absTotal + pixels relParts.append(relPart) absParts.append(pixels) col = col.__next__ # Ok, now we have the relative widths and absolute widths in # two parallel arrays. # # - If there are no relative widths, output the absolute widths # - If there are no absolute widths, output the relative widths # - If there are a mixture of relative and absolute widths, # - If the table width is absolute, turn these all into absolute # widths. # - If the table width is relative, turn these all into absolute # widths in the nominalWidth and then turn them back into # percentages. widths = [] if relTotal == 0: for absPart in absParts: if foStylesheet: inches = absPart / pixelsPerInch widths.append("%4.2fin" % inches) else: widths.append("%d" % absPart) elif absTotal == 0: for relPart in relParts: rel = relPart / relTotal * 100 widths.append(rel) widths = correctRoundingError(widths) else: pixelWidth = nominalWidth if string.find(tableWidth, "%") < 0: pixelWidth = convertLength(tableWidth) if pixelWidth <= absTotal: print("Table is wider than table width") else: pixelWidth = pixelWidth - absTotal absTotal = 0 for count in range(len(relParts)): rel = relParts[count] / relTotal * pixelWidth relParts[count] = rel + absParts[count] absTotal = absTotal + rel + absParts[count] if string.find(tableWidth, "%") < 0: for count in range(len(relParts)): if foStylesheet: pixels = relParts[count] inches = pixels / pixelsPerInch widths.append("%4.2fin" % inches) else: widths.append(relParts[count]) else: for count in range(len(relParts)): rel = relParts[count] / absTotal * 100 widths.append(rel) widths = correctRoundingError(widths) # Danger, Will Robinson! In-place modification of the result tree! # Side-effect free? We don' need no steenkin' side-effect free! count = 0 col = colChildren while col != None: if foStylesheet: col.setProp("column-width", widths[count]) else: col.setProp("width", widths[count]) count = count+1 col = col.__next__ return nodeset
def ExtractContexts(ctx): parser_ctx = libxslt.xpathParserContext(_obj=ctx).context() transform_ctx = parser_ctx.transformContext() return parser_ctx, transform_ctx