def graphviz(key, value, fmt, meta):
    if key == 'CodeBlock':
        [[ident, classes, keyvals], code] = value
        caption = ""
        if "plantuml" in classes:
            path = os.path.dirname(os.path.abspath(__file__))
            filename = sha1(code)
            alt = Str(caption)
            tit = ""
            src = imagedir + '/' + filename + '.png'
            out(src)
            if not os.path.isfile(src):
                try:
                    os.mkdir(imagedir)
                except OSError:
                    pass
                data, err = pipe(["plantuml", "-pipe", "-Tpng"], code)
                if (len(err) > 0):
                    return Para([Str(err)])
                with open(src, 'w') as f:
                    f.write(data)
            try:
                image = Image(attributes({}), [alt], [src, tit])
                return Para([image])
            except:
                try:
                    image = Image([alt], [src, tit])
                    return Para([image])
                except:
                    pass
Ejemplo n.º 2
0
def todo(key, value, fmt, meta):
    global stringbuffer, in_todo
    # order is important here!
    if key == 'Str' and "}" in value:
        in_todo = False
        stringbuffer += value
        idx_end = stringbuffer.index("}")
        idx_start = stringbuffer.index("{")
        before = stringbuffer[:idx_start] if idx_start > 0 else None
        todo_str = stringbuffer[idx_start + 1:idx_end]
        after = stringbuffer[idx_end +
                             1] if idx_end < len(stringbuffer) - 1 else None
        # todo_str = "\\todo[inline]{%s}{}" % (stringbuffer.strip("{}"))
        todo_str = "\\fxnote{%s}" % (todo_str)
        stringbuffer = ""
        blocks = []
        if before:
            blocks.append(Str(before))
        blocks.append(RawInline('latex', todo_str))
        if after:
            blocks.append(Str(after))
        return blocks
    if key == 'Str' and value.startswith("{"):
        in_todo = True
        stringbuffer += value
        return []  # remove it
    if key == 'Str' and in_todo:
        stringbuffer += value
        return []  # remove it
    if key == 'Space' and in_todo:
        stringbuffer += " "
        return []  # remove it
Ejemplo n.º 3
0
def graphviz(key, value, fmt, meta):
  if key == 'CodeBlock':
    [[ident,classes,keyvals], raw_code] = value
    caption = ""
    if "R" in classes:
      path = os.path.dirname(os.path.abspath(__file__))
      filename = sha1(raw_code)
      alt = Str(caption)
      tit = ""
      src = imagedir + '/' + filename + '.png'
      if not os.path.isfile(src):
        try:
            os.mkdir(imagedir)
        except OSError:
            pass
        code = "ppi <- 300\npng('" + src + \
                "', width=6*ppi, height=6*ppi, res=ppi)\n" + raw_code
        data, err = pipe(["R", "--no-save"], code)
        if (len(err) > 0):
            return Para([Str(err)])
      try:
        image = Image(attributes({}), [alt], [src,tit])
        return Para([image])
      except:
        try:
          image = Image([alt], [src,tit])
          return Para([image])
        except:
          pass
Ejemplo n.º 4
0
def action(key, value, fmt, meta):  # pylint: disable=unused-argument
    """Processes elements."""

    global has_lettrine  # pylint: disable=global-statement

    if key == 'Span':

        attrs = PandocAttributes(value[0], 'pandoc')

        if 'lettrine' in attrs.classes:

            has_lettrine = True

            firstword = value[1][0]['c']
            content = value[1][1:]

            # Replace span in para with the elements
            ret = [PRE, Str(firstword[0]), MID]
            if len(firstword) > 1:
                ret.append(Str(firstword[1:]))
            ret.append(POST)
            ret += content

            return  ret

    return None
Ejemplo n.º 5
0
    def _attach_attrs(x):
        """Extracts and attaches the attributes."""
        for i, v in enumerate(x):
            if v and v['t'] == elname:  # Find where the attributes start
                n = i + 1
                if allow_space and n < len(x) and x[n]['t'] == 'Space':
                    n += 1
                try:  # Extract the attributes
                    attrs = extract_attrs(x, n)
                    if _WARNINGLEVEL and attrs.parse_failed:
                        msg = textwrap.dedent("""
                            %s: Malformed attributes:
                            %s
                        """ % (_FILTERNAME, attrs.attrstr))
                        STDERR.write(msg)
                        STDERR.flush()

                    if replace:
                        x[i]['c'][0] = attrs.list
                    else:
                        x[i]['c'].insert(0, attrs.list)
                except (ValueError, IndexError):
                    if v['t'] == 'Span' and v['c'][0] is None:
                        # We changed this into a span before, but since
                        # the attributes are None (it was unattributed), it
                        # isn't a valid span.  Fix it.
                        els = x.pop(i)['c'][1]
                        els.insert(0, Str('['))
                        els.append(Str(']'))
                        for j, el in enumerate(els):
                            x.insert(i + j, el)
                        join_strings('Span', x)
                        return None
        return True
Ejemplo n.º 6
0
def wrap_urls_in_anchors(key, val, fmt, meta):
    if key == 'Para':
        children = []
        for child in val:
            new_objs = [child]
            if child['t'] == 'Str':
                s = child['c']
                match = URL_REGEX.search(s)
                if match:
                    link_text = match.group(1)
                    url = link_text
                    if not url.startswith('http'):
                        url = 'http://' + url

                    link = Link(['', [], []], [Str(link_text)], [url, ''])

                    new_objs = []
                    before, _, after = URL_REGEX.split(s, 1)
                    if before:
                        new_objs.append(Str(before))

                    new_objs.append(link)

                    if after:
                        new_objs.append(Str(after))

            children += new_objs

        return Para(children)
Ejemplo n.º 7
0
def parseMarks(key, value, format, meta):
    if key == 'Str':
        if regexes['all'].search(value):
            items = regexes['all'].split(value, 1)
            result = [Str(items[0]), RawInline('critic', items[1])]
            result.extend(walk([Str(items[2])], parseMarks, format, meta))
            return result
Ejemplo n.º 8
0
def filter(key, value, fmt, meta):
    if key == 'CodeBlock':
        value[1] = value[1].replace(b'\xef\xbf\xbd', '?')
        [[ident, classes, kvs], code] = value
        c = classes[0].split(',')[0]
        if c == 'rust':
            return mkListingsEnvironment(code)
    elif key == 'Link':
        [_, text, [href, _]] = value
        if text == [Str("include")]:
            return mkInputListings(href)
        elif (not href.startswith("http")) and href.endswith(".md"):
            src = re.search(r'(?:./)?(.+\.md)', href).group(1)
            return mkRef(src)
    elif key == 'Image':
        [_, _, [src, _]] = value
        if src.startswith("http"):
            fileName = src.split("/")[-1]
            os.system("cd img && curl -O " + src)
            return mkIncludegraphics(fileName)
    elif key == 'Str':
        return (Str(value.replace(b'\xef\xbf\xbd', '?').replace(u"〜", u"~")))
    elif key == 'Code':
        value[1] = value[1].replace(b'\xef\xbf\xbd', '?')
    elif key == 'Header':
        [level, _, _] = value
        if level == 1:
            file_name = os.getenv('FILENAME', "FILE_DOES_NOT_EXIST")
            value[1][0] = file_name
    elif key == 'RawInline':
        [t, s] = value
        if t == 'html' and '<img' in s:
            src = re.search(r'src="img/(.+?)"', s).group(1)
            return mkIncludegraphics(src)
Ejemplo n.º 9
0
def filter(key, value, fmt, meta):
    if key == 'CodeBlock':
        [[ident, classes, kvs], code] = value
        if 'scala' in classes:
            return mkListingsEnvironment(code)
    elif key == 'Link':
        [_, text, [href, _]] = value
        if text == [Str("include")]:
            return mkInputListings(href)
        elif href.endswith(".md"):
            src = re.search(r'(?:./)?(.+\.md)', href).group(1)
            return mkRef(src)
    elif key == 'Image':
        [_, _, [src, _]] = value
        if src.startswith("http"):
            fileName = src.split("/")[-1]
            os.system("cd images && wget " + src)
            return mkIncludegraphics(fileName)
    elif key == 'Str':
        return(Str(value.replace(u"〜", u"~")))
    elif key == 'Header':
        [level, _, _] = value
        if level == 1:
            file_name = os.getenv('FILENAME', "FILE_DOES_NOT_EXIST")
            value[1][0] = file_name
def pygments(key, value, format, _):

    if format == "asciidoc":

        # Fix references to figures
        if (key == "Str") and value.startswith("@ref"):
            # stderr.write(f"{key}\t{value}\n")
            _, ref_type, ref_id, _ = re.split("\(|:|\)", value)
            return Str(f"<<{ref_type}:{ref_id}>>")

        elif key == "Div":
            [[ident, classes, keyvals], code] = value
            div_type = classes[0]

            # Fix admonition
            if div_type.startswith("rmd"):
                adm_type = div_type[3:]
                return Plain([Str(f"[{ADM_TYPE[adm_type]}]\n====\n")] +
                             code[0]["c"] + [Str("\n====\n\n")])

            # Fix figures
            elif div_type == "figure":
                fig_id = code[2]["c"][0]["c"].split(")")[0][2:]
                html = code[0]["c"][0]["c"][1]
                stderr.write(f"{html}\n")
                _, src, _, alt, *_ = html.split("\"")
                return Plain(
                    [Str(f"[[{fig_id}]]\n.{alt}\nimage::{src}[{alt}]")])

    elif format == "html4":

        # Turn text callout number into unicode char
        if (key == "Str") and (match := callout_text_re.fullmatch(value)):
            num = int(match.group(1))
            br = "<br>" if num > 1 else ""
            return RawInline(
                "html", f"{br}<span class=\"callout\">&#{num + 10121};</span>")

        # Insert "Figure" or "Example" in front of internal references
        if (key == "Str") and value.startswith("@ref"):
            _, ref_type, ref_id, _ = re.split("\(|:|\)", value)
            return Str(f"{REF_TYPE[ref_type]} {value}")

        elif key == "CodeBlock":
            [[ident, classes, keyvals], code] = value
            if classes:
                language = classes[0]
                # stderr.write(f"{key}\t{value}\t{format}\n")
                result = "<pre>" + conv.convert(code, full=False) + "</pre>"

                # Turn code callout number into unicode char
                result = callout_code_re.sub(
                    lambda x:
                    f"<span class=\"callout\">&#{int(x.group(1))+10121};</span>",
                    result)
            else:
                result = code
            return RawBlock("html", result)
Ejemplo n.º 11
0
def unmathsup(key, value, format, meta):
	if (key == 'Math'
		and len(value) == 2
		and '^{' in (x[:2] for x in value if isinstance(x, str))
		and 'InlineMath' in (x['t'] for x in value if 't' in x)):
			for x in value:
				if isinstance(x, str):
					x = Str(x.split('^{')[1].split('}')[0])
					return Superscript([x])
Ejemplo n.º 12
0
def initialize(key, value, fmt, meta):
    global first
    if key == 'Str':
        if first:
            first = False
            return ([Str(value[0] + '.')])
        else:
            first = True
            return (Str(value))
Ejemplo n.º 13
0
    def wrap_image_output(self, chunk_name, data, key, attrs):
        '''
        Extra handling for images

        Parameters
        ----------
        chunk_name, data, key : str
        attrs: dict

        Returns
        -------
        Para[Image]
        '''

        # TODO: interaction of output type and standalone.
        # TODO: this can be simplified, do the file-writing in one step
        def b64_encode(data):
            return base64.encodestring(data.encode('ascii')).decode('ascii')

        # TODO: dict of attrs on Stitcher.
        image_attrs = {'width', 'height'}

        def transform_key(k):
            # fig.width -> width, fig.height -> height;
            return k.split('fig.', 1)[-1]

        attrs = [(transform_key(k), v) for k, v in attrs.items()
                 if transform_key(k) in image_attrs]

        if self.self_contained:
            if 'png' in key:
                data = 'data:image/png;base64,{}'.format(data)
            elif 'svg' in key:
                data = 'data:image/svg+xml;base64,{}'.format(b64_encode(data))
            if 'png' in key or 'svg' in key:
                block = Para(
                    [Image([chunk_name, [], attrs], [Str("")], [data, ""])])
            else:
                raise TypeError("Unknown mimetype %s" % key)
        else:
            # we are saving to filesystem
            filepath = os.path.join(self.resource_dir,
                                    "{}.png".format(chunk_name))
            with open(filepath, 'wb') as f:
                f.write(base64.decodestring(data.encode('ascii')))
            # Image :: alt text (list of inlines), target
            # Image :: Attr [Inline] Target
            # Target :: (string, string)  of (URL, title)
            block = Para([
                Image([chunk_name, [], []], [Str("")],
                      [filepath, "fig: {}".format(chunk_name)])
            ])

        return block
Ejemplo n.º 14
0
def protect_quote(key, value, fmt, _meta):
    """Protect U+2019 against https://github.com/jgm/pandoc/issues/4550"""
    if fmt == "ms":
        if key == 'Str':
            if re.search(r'[’]', value):
                strs = re.split(r'[’]', value)
                ret = [Str(strs[0])]
                for s in strs[1:]:
                    ret += [RawInline("ms", "’"), Str(s)]
                return ret
    return None  # change nothing
Ejemplo n.º 15
0
def filter(key, value, fmt, meta):
    if key == 'CodeBlock':
        value[1] = value[1].replace(b'\xef\xbf\xbd', '?')
        [[ident, classes, kvs], code] = value
        c = classes[0].split(',')[0]
        if c == 'rust':
            return mkListingsEnvironment(code)
    elif key == 'Link':
        [_, text, [href, _]] = value
        if text == [Str("include")]:
            return mkInputListings(href)
        elif (not href.startswith("http")) and href.endswith(".md"):
            src = re.search(r'(?:./)?(.+\.md)', href).group(1)
            return mkRef(src)
    elif key == 'Image':
        [_, _, [src, _]] = value
        if src.startswith("http"):
            fileName = src.split("/")[-1]
            os.system("cd img && curl -O " + src)
            return mkIncludegraphics(fileName)
    elif key == 'Str':
        return (Str(value.replace(b'\xef\xbf\xbd', '?').replace(u"〜", u"~")))
    elif key == 'Code':
        value[1] = value[1].replace(b'\xef\xbf\xbd', '?')
    elif key == 'Header':
        [level, _, _] = value
        if level == 1:
            file_name = os.getenv('FILENAME', "FILE_DOES_NOT_EXIST")
            value[1][0] = file_name
    elif key == 'RawInline':
        [t, s] = value
        if t == 'html' and '<img' in s:
            src = re.search(r'src="img/(.+?)"', s).group(1)
            return mkIncludegraphics(src)
        elif t == 'html' and s == '<sup>':
            return mkBeginSup()
        elif t == 'html' and s == '</sup>':
            return mkEndSup()
    elif key == 'Para':
        if value[0]['t'] == 'RawInline':
            fmt, content = value[0]['c']
            if fmt == 'html' and '<img' in content:
                src = re.search(r'src="(img/.+?)"', content).group(1)
                cls = re.search(r'class="(.+?)"', content)
                if cls:
                    cls = cls.group(1)
                width = re.search(r'style="width: *(\d+)%;?', content)
                if width:
                    width = float(width.group(1)) / 100
                return mkFigure(src, align=cls, scale=width)
            elif fmt == 'html' and 'class="caption"' in content:
                return [Para(value), RawBlock('latex', r'\vspace{1em}')]
            elif fmt == 'html' and 'class="filename"' in content:
                return [RawBlock('latex', r'\vspace{1em}'), Para(value)]
Ejemplo n.º 16
0
def replace_unit(key, value, format, meta):
    """
    Try to perform the replacements in `value` according to `pattern`.

    If nothing is replaced the this function does not return any value.

    Parameters
    ----------
    key : (string) The pandoc element type. Here we check if it is 'Str'.
    value : (string) The original text.
    format : (string) The output format of pandoc.
    meta : Not used here.
    """
    # sys.stderr.write(f"key is: {key} - value is: {value}\n")
    if key == 'Str':
        if format == 'org':
            replacement = r"\\nbsp{}"
            newValue = nbsp_pattern.sub(replacement, value)
            # sys.stderr.write(f"--- Value is {str(value)}, newValue is {str(newValue)}\n")
            if newValue != value:
                return RawInline("latex", newValue)
    elif key == 'Cite':
        if format == 'org':
            # A cite command can have several keys. The variable bibkeys here
            # will be a list with those keys
            bibkeys = [i["citationId"] for i in value[0]]
            return Str(f"cite:{','.join(bibkeys)}")
        # sys.stderr.write(f"key is: {key} - value is: {value}\n")
    elif key == 'Span':
        # This convet the usage of acronym package commands

        # sys.stderr.write(f"key is: {key} - value is: {value}\n")
        if value[0][2][0][0] == 'acronym-label':
            label = value[0][2][0][1]
            acronym_form = value[0][2][1][1]
            form_to_command = {'singular+short': 'ac', 'plural+short': 'acp',
                               'singular+full': 'acrfull', 'singular+abbrv': 'acrshort'}
            # sys.stderr.write(f"key is: {key} - value is: {value}\n")
            return Str(f"[[{form_to_command[acronym_form]}:{label}]]")
    elif key == 'Link':
        # In case the link is a reference (\ref{something} or \eqref{something})
        # replace its converted syntax from native org-mode to org-ref syntax
        if value[0][2][0][1] == 'ref':
            link_label = value[0][2][1][1].replace(":", "-")
            return Str(f"ref:{link_label}")
        elif value[0][2][0][1] == 'eqref':
            link_label = value[0][2][1][1].replace(":", "-")
            return Str(f"eqref:{link_label}")
    elif key == 'Header':
        # Replace ':' by '-' in the header label
        value[1][0] = value[1][0].replace(":", "-")
        pass
Ejemplo n.º 17
0
def replace_cite_references(key, val, fmt, meta):
    if key == 'Cite':
        label = val[0][0]['citationId']
        if label and label in label_map:
            ref_string, ref_id, prev_strings = label_map[label]
            return [
                Link(['', ['engrafo-cite'], []], [Str(ref_string)],
                     ['#%s' % ref_id, ''])
            ]
        # TODO: below doesn't work yet
        else:
            return Span(['', ['engrafo-cite', 'engrafo-missing-cite'], []],
                        [Str('[?]')])
Ejemplo n.º 18
0
    def _cite_replacement(key, value, fmt, meta):
        """Returns context-dependent content to replace a Cite element."""

        assert key == 'Cite'

        attrs, label = value[0], _get_label(key, value)
        attrs = PandocAttributes(attrs, 'pandoc')

        assert label in references

        # Get the replacement value
        text = str(references[label])

        # Choose between \Cref, \cref and \ref
        use_cleveref = attrs['modifier'] in ['*', '+'] \
          if 'modifier' in attrs.kvs else use_cleveref_default
        plus = attrs['modifier'] == '+' if 'modifier' in attrs.kvs \
          else use_cleveref_default
        name = plusname[0] if plus else starname[0]  # Name used by cref

        # The replacement depends on the output format
        if fmt == 'latex':
            if use_cleveref:
                # Renew commands needed for cleveref fakery
                if not 'xnos-cleveref-fake' in meta or \
                  get_meta(meta, 'xnos-cleveref-fake'):
                    faketex = (r'\xrefname' if plus else r'\Xrefname') + \
                      '{%s}' % name
                else:
                    faketex = ''
                macro = r'\cref' if plus else r'\Cref'
                ret = RawInline('tex', r'%s%s{%s}'%(faketex, macro, label))
            elif use_eqref:
                ret = RawInline('tex', r'\eqref{%s}'%label)
            else:
                ret = RawInline('tex', r'\ref{%s}'%label)
        else:
            if use_eqref:
                text = '(' + text + ')'

            linktext = [Math({"t":"InlineMath", "c":[]}, text[1:-1]) \
               if text.startswith('$') and text.endswith('$') \
               else Str(text)]

            link = elt('Link', 2)(linktext, ['#%s' % label, '']) \
              if _PANDOCVERSION < '1.16' else \
              Link(['', [], []], linktext, ['#%s' % label, ''])
            ret = ([Str(name), Space()] if use_cleveref else []) + [link]

        return ret
Ejemplo n.º 19
0
def _repair_refs(x):
    """Performs the repair on the element list `x`."""

    if not bool(_PANDOCVERSION):
        raise RuntimeError('Module uninitialized.  Please call init().')

    # Scan the element list x
    for i in range(len(x) - 1):

        # Check for broken references
        if _is_broken_ref(x[i]['t'], x[i]['c'] if 'c' in x[i] else [],
                          x[i + 1]['t'],
                          x[i + 1]['c'] if 'c' in x[i + 1] else []):

            # Get the reference string
            n = 0 if version(_PANDOCVERSION) < version('1.16') else 1
            s = x[i]['c'][n][0]['c'] + x[i + 1]['c']

            # Chop it into pieces.  Note that the prefix and suffix may be
            # parts of other broken references.
            prefix, label, suffix = _REF.match(s).groups()

            # Insert the suffix, label and prefix back into x.  Do it in this
            # order so that the indexing works.
            if suffix:
                x.insert(i + 2, Str(suffix))
            x[i + 1] = Cite([{
                "citationId": label,
                "citationPrefix": [],
                "citationSuffix": [],
                "citationNoteNum": 0,
                "citationMode": {
                    "t": "AuthorInText",
                    "c": []
                },
                "citationHash": 0
            }], [Str('@' + label)])
            if prefix:
                if i > 0 and x[i - 1]['t'] == 'Str':
                    x[i - 1]['c'] = x[i - 1]['c'] + prefix
                    del x[i]
                else:
                    x[i] = Str(prefix)
            else:
                del x[i]

            return None  # Forces processing to repeat

    return True  # Terminates processing
Ejemplo n.º 20
0
def pygments(key, value, format, _):

    # if key not in ["Space", "Str", "RawInline", "Para", "Quoted", "Plain"]:
    # stderr.write(f"- {key}: {value[:100]}\n")

    # if (key == "Str") and "fig:" in value:
    #     stderr.write(f"{key}\t{value}\t{format}\n")

    if format == "asciidoc":

        # Fix references to figures
        if (key == "Str") and value.startswith("@ref"):
            # stderr.write(f"{key}\t{value}\n")
            _, ref_type, ref_id, _ = split("\(|:|\)", value)
            return Str(f"<<{ref_type}:{ref_id}>>")

        elif key == "Div":
            [[ident, classes, keyvals], code] = value
            div_type = classes[0]

            # Fix admonition
            if div_type.startswith("rmd"):
                adm_type = div_type[3:]
                return Plain([Str(f"[{ADM_TYPE[adm_type]}]\n====\n")] +
                             code[0]["c"] + [Str("\n====\n\n")])

            # Fix figures
            elif div_type == "figure":
                fig_id = code[2]["c"][0]["c"].split(")")[0][2:]
                html = code[0]["c"][0]["c"][1]
                _, src, _, alt, _ = html.split("\"")
                return Plain(
                    [Str(f"[[{fig_id}]]\n.{alt}\nimage::{src}[{alt}]")])

    elif format == "html4":

        # Insert "Figure" or "Example" in front of internal references
        if (key == "Str") and value.startswith("@ref"):
            _, ref_type, ref_id, _ = split("\(|:|\)", value)
            return Str(f"{REF_TYPE[ref_type]} {value}")

        # Highlight code using pygments
        elif key == "CodeBlock":
            [[ident, classes, keyvals], code] = value
            language = classes[0]
            # stderr.write(f"\n\nformat: {format}\n\n```" + language + "\n" + code + "\n```\n\n\n")
            result = highlight(code, get_lexer_by_name(language),
                               HtmlFormatter())
            return RawBlock("html", result)
Ejemplo n.º 21
0
def makeCriticPart(kind, attr, contents):
    if kind == 'insertion':
        start = Str('{++')
        end = Str('++}')
    else:
        start = Str('{--')
        end = Str('--}')
    author = findValue('author', attr)
    date = findValue('date', attr)
    comment = RawInline('html', "{>>author: %s, date: %s<<}" % (author, date))
    result = [start]
    result.extend(contents)
    result.append(end)
    result.append(comment)
    return result
Ejemplo n.º 22
0
def behead(key, value, format, meta):
  global last_element
  # pandoc does not emit labels before sections -> insert
  if key == 'Header':
    lbl = value[1][0]
    if lbl:
      new_lbl = ".. _" + lbl + ":\n\n"
      value[1][0] = ""
      store(key, value)
      return [rb(new_lbl), Header(value[0], value[1], value[2])]
  # fix two bugs with string parsing
  elif key == 'Str':
    # pandoc does not parse \xpsace correctly -> insert whitespace after CAF
    if last_element == ('Str', 'CAF') and value.isalnum():
      store(key, value)
      return [Space(), Str(value)]
    if len(value) > 3:
      # pandoc generates [refname] as strings for \ref{refname} -> fix
      if value[0] == '[':
        store(key, value)
        return mk_ref(value[1:-1])
      elif value[1] == '[':
        store(key, value)
        return mk_ref(value[2:-1])
  # images don't have file endings in .tex -> add .png
  elif key == 'Image':
    store(key, value)
    return Image(value[0], value[1], [value[2][0] + ".png", value[2][1]])
  store(key, value)
Ejemplo n.º 23
0
def graphviz(key, value, format, meta):
    if key == 'CodeBlock':
        [[ident, classes, keyvals], code] = value
        caption = "caption"

        if "graphviz" in classes:
            G = pygraphviz.AGraph(string=code)
            layout = "dot"
            for elem in keyvals:
                if elem[0] == "layout":
                    layout = elem[1]
            G.layout(layout)
            filename = sha1(code)
            if format == "html":
                filetype = "png"
            elif format == "latex":
                filetype = "pdf"
            else:
                filetype = "png"
            alt = Str(caption)
            src = imagedir + '/' + filename + '.' + filetype
            if not os.path.isfile(src):
                try:
                    os.mkdir(imagedir)
                except OSError:
                    pass
                G.draw(src)
                out(src)
            tit = ""
            return Para([Image(['', [], []], [alt], [src, tit])])
def ruby_kenten(key, val, fmt, meta):
    ruby_pattern = r'(?:(?:[\||](?:\p{Hiragana}|\p{Katakana}|\p{Han}|ー|\p{P}|█)+?)|(?:\p{Han}+?))《(?!.*《).*?》'
    kenten_pattern = r'《《(?:\p{Hiragana}|\p{Katakana}|\p{Han}|\p{P}|ー)+?》》'
    if key == 'Header':
        val[1][0] = val[2][0]['c']
        return Header(val[0], val[1], val[2])
    if key != 'Str':
        return
    filtered_val = val
    for matched_vals in regex.findall(ruby_pattern, filtered_val):
        base = regex.search(r'(((?<=[\||])(.*?)(?=《))|(\p{Han}*?(?=《)))',
                            matched_vals).groups(1)[0]
        ruby = regex.search(r'((?<=《)(.*?)(?=》))', matched_vals).groups(1)[1]
        filtered_ruby = regex.search(
            r'^((.*?)(?=[\||]))', ruby)[0] if regex.search(
                r'(.*)?[\||](?!.*《)(?!.*[\||])', ruby) else ruby
        for grouped_ruby in regex.findall(
                r'(((?<=[\||])(.*?)(?=[\||]))|((?<=[\||])(.*)(?=$)))', ruby):
            if fmt == 'latex':
                filtered_ruby = r'%s|%s' % (filtered_ruby, grouped_ruby[0])
            elif fmt in ('html', 'html5', 'epub', 'epub3'):
                filtered_ruby = r'%s%s' % (filtered_ruby, grouped_ruby[0])
        ruby = filtered_ruby
        if fmt == 'latex':
            filtered_str = r'\\ruby{%s}{%s}' % (base, ruby)
        elif fmt in ('html', 'html5', 'epub', 'epub3'):
            filtered_str = (r'<ruby><rb>%s</rb><rp>'
                            '《</rp><rt>%s</rt><rp>》'
                            '</rp></ruby>') % (base, ruby)
        filtered_val = regex.sub(r'%s' % matched_vals, r'%s' % filtered_str,
                                 filtered_val)
    for matched_vals in regex.findall(kenten_pattern, filtered_val):
        base = regex.search(r'《《(.+?)》》', matched_vals).groups(0)[0]
        if fmt == 'latex':
            filtered_str = r'\\kenten{%s}' % base
        elif fmt in ('html', 'html5', 'epub', 'epub3'):
            kenten = ''
            for kenten_count in base:
                kenten += r'・'
            filtered_str = (r'<ruby><rb>%s</rb><rp>'
                            '《</rp><rt>%s</rt><rp>》'
                            '</rp></ruby>') % (base, kenten)
        filtered_val = regex.sub(r'%s' % matched_vals, r'%s' % filtered_str,
                                 filtered_val)

    if fmt == 'latex':
        for matched_vals in regex.findall(r'…', filtered_val):
            filtered_val = regex.sub(r'%s' % matched_vals, r'…', filtered_val)
        for matched_vals in regex.findall(r'―', filtered_val):
            filtered_val = regex.sub(r'%s' % matched_vals, r'—', filtered_val)

    filtered_val = regex.sub(r'[\||]《', r'《', filtered_val)

    if 'matched_vals' in locals():
        if fmt == 'latex':
            return RawInline('latex', r'%s' % filtered_val)
        if fmt in ('html', 'html5', 'epub', 'epub3'):
            return RawInline('html', r'%s' % filtered_val)
    else:
        return Str(filtered_val)
Ejemplo n.º 25
0
def remove_references(key, value, format, meta):
    if key == 'Link':
        pass
        if value[1][0]['t'] == 'Code':
            return value[1][0]
        return Str(' '.join([e['c'] for e in value[1] if 'c' in e.keys()]))
    return None
Ejemplo n.º 26
0
    def _quotify(key, value, fmt, meta):  # pylint: disable=unused-argument
        """Replaced Quoted elements with quoted strings."""
        if key == 'Quoted':
            ret = []
            quote = '"' if value[0]['t'] == 'DoubleQuote' else "'"
            if value[1][0]['t'] == 'Str':
                value[1][0]['c'] = quote + value[1][0]['c']
            else:
                ret.append(Str(quote))

            if value[1][-1]['t'] == 'Str':
                value[1][-1]['c'] = value[1][-1]['c'] + quote
                ret += value[1]
            else:
                ret += value[1] + [Str(quote)]
            return ret
Ejemplo n.º 27
0
def plantuml(key, value, fmt, meta):
    if key == 'CodeBlock':
        [[ident, classes, keyvals], code] = value
        if "plantuml" in classes:
            caption, typef, keyvals = get_caption(keyvals)
            path = os.path.dirname(os.path.abspath(__file__))
            filename = sha1(code)
            src = imagedir + '/' + filename + '.png'
            if not os.path.isfile(src):
                try:
                    os.mkdir(imagedir)
                except OSError:
                    pass
                data, err = pipe(["plantuml", "-pipe", "-Tpng"], code)
                if (len(err) > 0):
                    return Para([Str(err)])
                with open(src, 'wb') as f:
                    f.write(data)
            try:
                image = Image([ident, [], keyvals], caption, [src, typef])
                return Para([image])
            except:
                try:
                    image = Image(caption, [src, typef])
                    return Para([image])
                except:
                    pass
Ejemplo n.º 28
0
def literal_to_AST_node ( text ):
    ret_val = []
    pieces = text.split ( ' ' )
    for piece in pieces:
        ret_val.append ( Str(piece) )
        ret_val.append ( Space() )
    return ret_val[:-1]
Ejemplo n.º 29
0
def R(key, value, fmt, meta):
    if key == 'CodeBlock':
        [[ident, classes, keyvals], raw_code] = value
        if "r" in classes:
            path = os.path.dirname(os.path.abspath(__file__))
            caption, typef, keyvals = get_caption(keyvals)
            width, keyvals = get_value(keyvals, "width", 7)
            height, keyvals = get_value(keyvals, "height", 7)
            filename = sha1(raw_code + str(width) + str(height))
            src = imagedir + '/' + filename + '.png'
            if not os.path.isfile(src):
                try:
                    os.mkdir(imagedir)
                except OSError:
                    pass
                code = "ppi <- 100\npng('" + src + \
                        "', width=" + width + "*ppi, height=" + height + "*ppi, res=ppi)\n" + raw_code
                data, err = pipe(["R", "--no-save"], code)
                if (len(err) > 0):
                    return Para([Str(err)])
            try:
                image = Image([ident, [], keyvals], caption, [src, typef])
                return Para([image])
            except:
                try:
                    image = Image(caption, [src, typef])
                    return Para([image])
                except:
                    pass
Ejemplo n.º 30
0
def insert_table_labels(key, val, fmt, meta):
    '''
    Insert "Table 3:" style prefixes before table captions
    and wrap in span with id=table-3 etc.
    '''
    if key == 'Table':
        caption = val[0]
        for i, obj in enumerate(caption):
            if obj['t'] == 'Span':
                span_val = obj['c'][0][2]
                if (len(span_val) == 1 and len(span_val[0]) == 2
                        and span_val[0][0] == 'data-label'):
                    label = span_val[0][1]
                    index = incr_latest_index('table')
                    ref_index = 'table-%d' % index

                    label_map[label] = Label(
                        ref_string='Table %d' % index,
                        ref_index=ref_index,
                        prev_strings=['table', 'tab.'],
                    )

                    span_index = i
                    caption.pop(span_index)
                    caption.insert(0, Str('Table %d: ' % index))
                    return Div([ref_index, ['engrafo-table'], []],
                               [Table(*val)])