Ejemplo n.º 1
0
def links(key, value, format, meta):
    if key == 'Link':
        [_, title, target] = value
        if (is_absolute(target[0])):
            # citation = [{"citationSuffix"  : [],
            #              "citationNoteNum" : 0,
            #              "citationMode"    : {"t":"NormalCitation"},
            #              "citationPrefix"  : [],
            #              "citationId"      : target[0],
            #              "citationHash"    : 0}]
            # return Cite(citation, title)
            return Span(attributes({}), [
                Str(u'\u201c'),
                Span(attributes({}), title),
                Str(u'\u201d'),
                Space(),
                Str('('),
                Str(target[0]),
                Str(')')
            ])
        else:
            [_, _, targetInternal] = target[0].rpartition('#')
            citation = [{
                "citationSuffix": [],
                "citationNoteNum": 0,
                "citationMode": {
                    "t": "NormalCitation"
                },
                "citationPrefix": [],
                "citationId": targetInternal,
                "citationHash": 0
            }]
            return Cite(citation, [Str("[@{0}]".format(targetInternal))])
Ejemplo n.º 2
0
def reformat_spaced_repetition_text(key, value, format, meta):
    if key == 'Para':
        # the spaced repetition text will always start with 'Q:'
        first_word = value[0]['c']
        if first_word == 'Q:':
            # the content of a paragraph is a list of inline elements
            # e.g. links, line breaks, in-line code snippets, text
            started_parsing_answer: bool = False

            para_question = []
            para_answer = []

            for inline in value:
                # there's always a soft line break before the 'A:'
                # once we've seen the soft break, we want to start populating
                # the list containing the inlines that make up the answer
                if inline['t'] == 'SoftBreak':
                    started_parsing_answer = True

                if started_parsing_answer:
                    para_answer.append(inline)
                else:
                    para_question.append(inline)

            # see constructor doc: https://github.com/jgm/pandocfilters/blob/master/pandocfilters.py
            return [
                Div(attributes({'class': 'question'}), [Plain(para_question)]),
                Div(attributes({'class': 'answer'}), [Plain(para_answer)])
            ]
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 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'
      out(src);
      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
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.º 6
0
def blockdiag(key, value, fmt, meta):
  if key == 'CodeBlock':
    [[ident,classes,keyvals], code] = value
    caption = ""
    found, cmd = isDiag(classes)
    if found == True:
      path = os.path.dirname(os.path.abspath(__file__))
      filename = sha1(code)
      alt = Str(caption)
      tit = ""
      src = imagedir + '/' + filename + '.png'
      if not os.path.isfile(src):
        try:
            os.mkdir(imagedir)
        except OSError:
            pass
        tmp = save(code)
        p = subprocess.Popen([cmd, "-Tpng", "-a", tmp, "-o", src],
                shell=False, stdin=None, stdout=None, stderr=subprocess.PIPE, close_fds=True)
        err = p.stderr.read()
        p.stderr.close()
        if (len(err) > 0):
            return Para([Str(err)])
        os.remove(tmp)
      try:
        image = Image(attributes({}), [alt], [src,tit])
        return Para([image])
      except:
        try:
          image = Image([alt], [src,tit])
          return Para([image])
        except:
          pass
Ejemplo n.º 7
0
def graphviz(key, value, fmt, meta):
    if key == 'CodeBlock':
        [[ident,classes,keyvals], code] = value
        caption = ""
        if "dot" in classes:
            path = os.path.dirname(os.path.abspath(__file__))
            filename = sha1(code)
            alt = Str(caption)
            tit = ""
            src = imagedir + '/' + filename + '.png'
            if not os.path.isfile(src):
                try:
                    os.mkdir(imagedir)
                except OSError:
                    pass
                data, err = pipe(["dot", "-Tpng", "-s100"], 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.º 8
0
def enclose_input_code(key, value, format, metadata):
    """Enclose any input code in a div to allow collapsing.

    'Input code' is a code block that has 'n' defined (the prompt
    number).
    """
    if key == 'CodeBlock':
        div_attr = {'classes': ['collapsible']}
        code_attr = PandocAttributes(value[0], format='pandoc')
        if 'n' not in code_attr.kvs:
            return
        else:
            button = pf.RawBlock('html', collapse_button(n=code_attr['n']))
            code = pf.CodeBlock(*value)
            content = [button, pf.Div(pf.attributes({}), [code])]
            return pf.Div(pf.attributes(div_attr), content)
Ejemplo n.º 9
0
def enclose_input_code(key, value, format, metadata):
    """Enclose any input code in a div to allow collapsing.

    'Input code' is a code block that has 'n' defined (the prompt
    number).
    """
    if key == 'CodeBlock':
        div_attr = {'classes': ['collapsible']}
        code_attr = PandocAttributes(value[0], format='pandoc')
        if 'n' not in code_attr.kvs:
            return
        else:
            button = pf.RawBlock('html', collapse_button(n=code_attr['n']))
            code = pf.CodeBlock(*value)
            content = [button, pf.Div(pf.attributes({}), [code])]
            return pf.Div(pf.attributes(div_attr), content)
def remove_attributes(blockquote):
    """Remove attributes from a blockquote if they are defined on a
    header that is the first thing in the blockquote.

    Modifies the blockquote inplace.
    """
    if blockquote[0]['t'] == 'Header':
        level, attr, inlines = blockquote[0]['c']
        attr = pf.attributes({})
        blockquote[0] = pf.Header(level, attr, inlines)
Ejemplo n.º 11
0
def remove_attributes(blockquote):
    """Remove attributes from a blockquote if they are defined on a
    header that is the first thing in the blockquote.

    Modifies the blockquote inplace.
    """
    if blockquote[0]['t'] == 'Header':
        level, attr, inlines = blockquote[0]['c']
        attr = pf.attributes({})
        blockquote[0] = pf.Header(level, attr, inlines)
Ejemplo n.º 12
0
def metavars(key, value, format, meta):
    if key == 'Str':
        m = pattern.match(value)
        if m:
            field = m.group(1)
            result = meta.get(field, {})
            if 'MetaInlines' in result['t']:
                return Span(attributes({'class': 'interpolated',
                                        'field': field}),
                            result['c'])
            elif 'MetaString' in result['t']:
                return Str(result['c'])
Ejemplo n.º 13
0
def links(key, value, format, meta):
    if key == 'Link':
        [_, title, target] = value
        if (is_absolute(target[0])):
            # citation = [{"citationSuffix"  : [],
            #              "citationNoteNum" : 0,
            #              "citationMode"    : {"t":"NormalCitation"},
            #              "citationPrefix"  : [],
            #              "citationId"      : target[0],
            #              "citationHash"    : 0}]
            # return Cite(citation, title)
            return Span(attributes({}),
                        [Str(u'\u201c'), Span(attributes({}), title), Str(u'\u201d'),
                         Space(), Str('('), Str(target[0]), Str(')')])
        else:
            [_, _, targetInternal] = target[0].rpartition('#')
            citation = [{"citationSuffix"  : [],
                         "citationNoteNum" : 0,
                         "citationMode"    : {"t":"NormalCitation"},
                         "citationPrefix"  : [],
                         "citationId"      : targetInternal,
                         "citationHash"    : 0}]
            return Cite(citation, [Str("[@{0}]".format(targetInternal))])
Ejemplo n.º 14
0
def interpretManLinks(key, value, fmt, meta):
    if key == 'Link':
        text, link = value
        url, title = link
        match = _man_link_re.match(url)
        if match is not None:
            str = stringify(text)
            if str.startswith("lenskit"):
                return text
            else:
                rv = Span(attributes(None),
                          text + [Str(" ("), Strong([Str(match.group(1))]), Str("(%s))" % (match.group(2),))])
                return rv
        else:
            return None
def create_figures(key, value, format, metadata):
    """Convert Images with attributes to Figures.

    Images are [caption, (filename, title)].

    Figures are [caption, (filename, title), attrs].

    This isn't a supported pandoc type, we just use it internally.
    """
    if isattrfigure(key, value):
        image = value[0]
        attr_string = pf.stringify(value[1:])

        caption, target = image['c']
        attrd = attr_parser.parse(attr_string)
        attrs = pf.attributes(attrd)

        return Figure(caption, target, attrs)
Ejemplo n.º 16
0
def interpretManLinks(key, value, fmt, meta):
    if key == 'Link':
        text, link = value
        url, title = link
        match = _man_link_re.match(url)
        if match is not None:
            str = stringify(text)
            if str.startswith("lenskit"):
                return text
            else:
                rv = Span(
                    attributes(None), text + [
                        Str(" ("),
                        Strong([Str(match.group(1))]),
                        Str("(%s))" % (match.group(2), ))
                    ])
                return rv
        else:
            return None
Ejemplo n.º 17
0
def blockdiag(key, value, fmt, meta):
    if key == 'CodeBlock':
        [[ident, classes, keyvals], code] = value
        caption = ""
        found, cmd = isDiag(classes)
        if found == True:
            path = os.path.dirname(os.path.abspath(__file__))
            filename = sha1(code)
            alt = Str(caption)
            tit = ""
            src = imagedir + '/' + filename + '.png'
            if not os.path.isfile(src):
                try:
                    os.mkdir(imagedir)
                except OSError:
                    pass
                tmp = save(code)
                p = subprocess.Popen([cmd, "-Tpng", "-a", tmp, "-o", src],
                                     shell=False,
                                     stdin=None,
                                     stdout=None,
                                     stderr=subprocess.PIPE,
                                     close_fds=True)
                err = p.stderr.read()
                p.stderr.close()
                if (len(err) > 0):
                    return Para([Str(err)])
                os.remove(tmp)
            try:
                image = Image(attributes({}), [alt], [src, tit])
                return Para([image])
            except:
                try:
                    image = Image([alt], [src, tit])
                    return Para([image])
                except:
                    pass
Ejemplo n.º 18
0
def textohtml(key, value, format, meta):
    if key == 'RawInline':
        fmt, s = value
        if fmt == "tex":
            for x in trans:
                m = x['re'].match(s)
                if m:
                    return [
                        Span(attributes({'class': x['class']}), [
                            Str(x['cont'] if x['key'] ==
                                'Str' else m.group(x['cont']))
                        ]),
                        Space()
                    ]
            if cboxStart.match(s):
                return RawInline("html", "<span class='cbox'>")
            if cboxEnd.match(s):
                return RawInline("html", "</span>")
            if image.match(s):
                m = image.match(s)
                #                return Image([Str("description")], [m.group(1),""])  # works only for pandocfilters < 1.3.0
                return Image(
                    ['', [], []], [Str("description")],
                    [m.group(1), ""])  # should work for pandocfilter >= 1.3.0
Ejemplo n.º 19
0
def makeSpan(contents, classes="", author="", date=""):
    attrs = {'classes': classes.split(), 'author': author, 'date': date}
    return Span(attributes(attrs), contents)
Ejemplo n.º 20
0
    meta = doc[0]['unMeta']
    
    result = meta.get('bibliography', {})
    if result:
        bibliography_path = value_of_metadata(result)
    result = meta.get('csl', {})
    if result:
        csl_path = value_of_metadata(result)
    
    if bibliography_path == None or csl_path == None:
        raise Exception('Metadata variables must be set for both bibliography and csl.')
    
    # Parse the BibTeX database.
    bib_source = BibTeX(bibliography_path, encoding='utf-8')

    # load a CSL style
    bib_style = CitationStylesStyle(csl_path, validate=False)
    
    bibliography = CitationStylesBibliography(bib_style, bib_source, f)
        
    altered = walk(doc, citation_register, format, doc[0]['unMeta'])
    second = walk(altered, citation_replace, format, doc[0]['unMeta'])
    
    references = []
    for item, key in zip(bibliography.bibliography(), bibliography.keys):
        attrs = {'id': key, 'class':'h-cite'}
        references.append(Div(attributes(attrs),[Para([render(str(item))])]))
        
    second[1].extend(references) # add more paragraphs to the end of the main document list of blocks
    
    json.dump(second, sys.stdout)