예제 #1
0
def h_html_image(e, doc):
    """
    Add figure number for html images
    """
    if not isinstance(e, pf.Image) or doc.format != "html":
        return None
    if not e.identifier:
        return None
    name, number, file = get_full_label(e.identifier, doc)
    if number:
        e.content = pf.ListContainer(pf.Str(number + ": "), *e.content)
    return e
def format_image(image, doc):
    # type: (Image, Doc) -> Element
    """
    originally adapted from:
    `pandoc-fignos <https://github.com/tomduck/pandoc-fignos/>`_
    """
    if not isinstance(image, pf.Image):
        return None

    span = None
    if (isinstance(image.parent, pf.Span)
            and LABELLED_IMAGE_CLASS in image.parent.classes):
        span = image.parent

    if span is not None:
        identifier = span.identifier
        attributes = span.attributes
        #  classes = span.classes
    else:
        identifier = image.identifier
        attributes = image.attributes
        # classes = image.classes

    if doc.format in ("tex", "latex"):
        new_doc = Doc(pf.Para(*image.content))
        new_doc.api_version = doc.api_version
        if image.content:
            caption = pf.run_pandoc(json.dumps(new_doc.to_json()),
                                    args=["-f", "json", "-t",
                                          "latex"]).strip()
        else:
            caption = ""

        options = attributes.get("placement", "")
        size = ""  # max width set as 0.9\linewidth
        if "width" in attributes:
            width = convert_units(attributes["width"], "fraction")
            size = "width={0}\\linewidth".format(width)
        elif "height" in attributes:
            height = convert_units(attributes["height"], "fraction")
            size = "height={0}\\paperheight".format(height)

        if identifier:
            latex = LATEX_FIG_LABELLED.format(
                label=identifier,
                options=options,
                path=image.url,
                caption=caption,
                size=size,
            )
        else:
            latex = LATEX_FIG_UNLABELLED.format(options=options,
                                                path=image.url,
                                                caption=caption,
                                                size=size)

        return pf.RawInline(latex, format="tex")

    elif doc.format in ("rst", ):
        if not image.content.list:
            # If the container is empty, then pandoc will assign an iterative
            # reference identifier to it (image0, image1).
            # However, this iterator restarts for each markdown cell,
            # which can lead to reference clashes.
            # Therefore we specifically assign the identifier here, as its url
            # TODO does this identifier need to be sanitized?
            # (it works fine in the tests)
            identifier = image.url
            image.content = pf.ListContainer(pf.Str(str(identifier)))

        return image
        # TODO formatting and span identifier (convert width/height to %)

    elif doc.format in ("html", "html5"):
        if identifier:
            return _wrap_in_anchor(image, identifier)
        else:
            return image
        # TODO formatting, name by count
    else:
        return None
예제 #3
0
def title_hacks(elem, doc):
	if isinstance(elem, pf.Header):
		#Filter out special attributes @[list_of_comma_separated_attr]@
		if len(elem.content)>=3 and isinstance(elem.content[0],pf.Cite) and isinstance(elem.content[1],pf.Str):
			print(elem.content[0].content[0].text, file=sys.stderr)
			# Remove also the space ...
			text_to_process=elem.content[0].content[0].text+elem.content[1].text

			## Remove @
			text_to_process=text_to_process.replace("@","")

			## Add classes
			row=re.split(r",",text_to_process)

			for item in row:
				elem.classes.append(item) 

			## Remove space if necessary
			if isinstance(elem.content[2],pf.Space):
				elem.content=elem.content[3:]
			else:
				elem.content=elem.content[2:]

			print(sys.stderr,elem.classes, file=sys.stderr, end="")

		##Pick language specific stuff
		tag_found=False
		right_content=[]
		left_content=[]
		for item in elem.content:
			if isinstance(item, pf.Str) and ( item.text=="|||" or item.text==";;;"):
				tag_found=True
			elif tag_found:
				right_content.append(item)
			else:
				left_content.append(item)

		if tag_found:
			if doc.lang_id>=2:
				right_content.pop(0) ## Remove first space
				elem.content=right_content
			else:
				elem.content=left_content[:-1] ## Remove space at the end
	if isinstance(elem, pf.TableCell):
		#pf.debug("Entra")
		##Pick language specific stuff
		#pf.debug(elem)
		for x in elem.content:
			tag_found=False
			right_content=[]
			left_content=[]
			for item in x.content:  
				if isinstance(item, pf.Str) and item.text==";;;":
					tag_found=True
				elif tag_found:
					right_content.append(item)
				else:
					left_content.append(item)
				#if not isinstance(item, pf.Space): 
				#	pf.debug(item.text)
			if tag_found:
				if doc.lang_id>=2:
					right_content.pop(0) ## Remove first space
					x.content=right_content
				else:
					x.content=left_content[:-1] ## Remove space at the end
	elif type(elem) == pf.Table:
		caption=elem.caption
		#pf.debug(caption)
		## No caption
		if len(elem.caption)==0: 
			return 
		last_item=caption[-1]
		crossref=False

		if type(last_item)==pf.Str and last_item.text.find("{")!=-1:
			del caption[-1] ## Delete it
			with_attributes=True
		else:
			with_attributes=False

		##Pick language specific stuff
		tag_found=False
		right_content=pf.ListContainer()
		left_content=pf.ListContainer()

		if not with_attributes:
			if latex_format(doc.format) and type(caption[0])==pf.RawInline:
				## Let's see if pandoc-crossref passed first
				right_content.append(caption[0])
				left_content.append(caption[0])
				del caption[0]
				#pf.debug(right_content)
				crossref=True
			elif html_format(doc.format) and len(caption)>=2 and type(caption[1])==pf.Str and caption[1].text[-1]==':':
				## Let's see if pandoc-crossref passed first
				right_content.append(caption[0])
				right_content.append(caption[1])
				left_content.append(caption[0])
				left_content.append(caption[1])
				del caption[0]				
				del caption[1]
				crossref=True

		for item in caption:
			#pf.debug(item)
			if isinstance(item, pf.Str) and ( item.text=="|||" or item.text==";;;"):
				tag_found=True
				continue
			if tag_found:
				right_content.append(item)
			else:
				left_content.append(item)

		if tag_found:
			if doc.lang_id>=2:
				if not crossref:
					right_content.pop(0) ## Remove first space
				if with_attributes:	
					right_content.append(last_item)
				elem.caption=right_content
			else:
				if with_attributes:	
					left_content.append(last_item)
				else:
					del left_content[-1] # Remove space at the end			
				elem.caption=left_content #
		#pf.debug(elem.caption)
		return None
	elif type(elem) == pf.Div:
			if "title" in elem.attributes:
				elem.attributes["title"]=translate_string(elem.attributes["title"],doc)	
	elif type(elem) == pf.Span: #and "dual" in attributes:
		##Pick language specific stuff
		tag_found=False
		right_content=[]
		left_content=[]
		for item in elem.content:
			if isinstance(item, pf.Str) and ( item.text=="|||" or item.text==";;;"):
				tag_found=True
			elif tag_found:
				right_content.append(item)
			else:
				left_content.append(item)
		if tag_found:
			if doc.lang_id>=2:
				right_content.pop(0) ## Remove first space
				elem.content=right_content
			else:
				elem.content=left_content[:-1] ## Remove space at the end