예제 #1
0
def main():
    'main entry point'

    def walker(key, value, fmt, meta):
        'walk down the pandoc AST and invoke workers for CodeBlocks'

        if key == 'CodeBlock':
            return dispatch(value, fmt, meta).image()

        elif key == 'Div':
            # Process Div & try to merge its (subsequent) Image's
            [[ident, classes, kvs], blocks] = value
            if not "im_merge" in classes:
                return None  # keep it as-is

            classes = [x for x in classes if x != 'im_merge']

            rv = []
            for block in blocks:
                if block['t'] != 'CodeBlock':
                    rv.append(block)
                else:
                    elm = dispatch(block['c'], fmt, meta).image()
                    rest = mergeImages(rv, elm)
                    rv.extend(rest)

            return pf.Div([ident, classes, kvs], rv)

    dispatch = Handler(None, None, None)
    pf.toJSONFilter(walker)
예제 #2
0
def latex_json_filter():
    r""" A Pandoc filter for additional and custom LaTeX processing
    functionality.

    .. see: pandoc_utils.latex_prefilter
    """
    toJSONFilter(latex_prefilter)
예제 #3
0
def main():
    global config

    config = configparser.ConfigParser()
    config.read(global_vars._CONFIG)

    toJSONFilter(parse_site)
예제 #4
0
파일: run.py 프로젝트: katnegermis/dotfiles
def include(key, value, format, meta):
    if key == 'CodeBlock':
        ((id, classes, namevals), contents) = value
        files = [val for name, val in namevals if name == 'run']
        # Use only first file.
        with codecs.open(files[0], 'r', 'utf8') as f:
            # compile and run file with given arguments
        return CodeBlock([id, classes, namevals], contents)

if __name__ == "__main__":
    toJSONFilter(include)
예제 #5
0
def main():
    'main entry point'

    def walker(key, value, fmt, meta):
        'walk down the pandoc AST and invoke workers for CodeBlocks'
        if key == 'CodeBlock':
            worker = dispatch(value).get_prefs(meta)
            return worker.image(fmt)

    dispatch = Handler(None)
    pf.toJSONFilter(walker)
예제 #6
0
파일: R.py 프로젝트: tex/vimpreviewpandoc
    [[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

if __name__ == "__main__":
  toJSONFilter(R)
예제 #7
0
"""
Pandoc filter to convert spans with class="correction" to LaTeX
\mccorrect environments in LaTeX output, and leave as is in HTML
output.
"""

from pandocfilters import toJSONFilter, RawInline, Span


def latex(x):
    #LaTeX inline
    return RawInline('latex', x)


def corrections(key, value, format, meta):
    if key == 'Span':
        [[ident, classes, keyvals], contents] = value
        if "correction" in classes:
            if format == "latex":
                if ident == "":
                    label = ""
                else:
                    label = '\\label{' + ident + '}'
                return ([latex('\\colorbox[HTML]{CCCCFF}{')] + contents +
                        [latex('}')])


if __name__ == "__main__":
    toJSONFilter(corrections)
def main():
    toJSONFilter(mermaid)
#!/usr/bin/env python
from __future__ import print_function

from pandocfilters import toJSONFilter, Image, RawInline, Cite, Para, Header, Str, RawBlock, attributes, stringify
import os, sys


"""
Pandoc metadata filter. 

"""


def warning(*objs):
    print("WARNING: ", *objs, file=sys.stderr)


def context(s):
    return RawBlock("context", s)


def myheader(key, value, fmt, meta):

    if key == "Image" and fmt == "context":

        warning(value)


if __name__ == "__main__":
    toJSONFilter(myheader)
예제 #10
0
        [[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

if __name__ == "__main__":
    toJSONFilter(graphviz)
예제 #11
0
    global base_url, version
    global count

    if key == 'Math':

        # grab equation
        par = False
        if value[0]['t'] == 'InlineMath':
            formula = '$' + value[1] + '$'
            caption = value[1]
        else:
            par = True
            formula = r'\begin{align*}' + value[1] + r'\end{align*}'
            caption = ''

        # typeset formula
        count += 1
        latex(formula, count)

        # replace by image
        return Image(['', [], []], [Str(caption)],
                     [base_url + version + '/eqn_{}.png'.format(count), ''])


if __name__ == "__main__":

    count = 0
    # filter
    toJSONFilter(filter)
예제 #12
0

def plantuml(key, value, format, _):
    if key == 'CodeBlock':
        [[ident, classes, keyvals], code] = value

        if "plantuml" in classes:
            caption, typef, keyvals = get_caption(keyvals)

            filename = get_filename4code("plantuml", code)
            filetype = get_extension(format, "png", html="svg", latex="eps")

            src = filename + '.uml'
            dest = filename + '.' + filetype

            if not os.path.isfile(dest):
                txt = code.encode(sys.getfilesystemencoding())
                if not txt.startswith("@start"):
                    txt = "@startuml\n" + txt + "\n@enduml\n"
                with open(src, "w") as f:
                    f.write(txt)

                call(["java", "-jar", "plantuml.jar", "-t" + filetype, src])
                sys.stderr.write('Created image ' + dest + '\n')

            return Para([Image([ident, [], keyvals], caption, [dest, typef])])


if __name__ == "__main__":
    toJSONFilter(plantuml)
예제 #13
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

from pandocfilters import toJSONFilter

def process(key, value, format, meta):

	# find images and change their src paths to relative ones
	if key == 'Image':
		src = value[1][0]
		value[1][0] = '../images/' + src.split('/')[-1]

	# find links to old uploads and change their href paths to relative ones
	if key == 'Link':
		href = value[1][0]
		if 'programminghistorian.org/wp-content/uploads/' in href:
			value[1][0] = '../images/' + href.split('/')[-1]

if __name__ == "__main__":
	toJSONFilter(process)
예제 #14
0
        key     type of pandoc object
        value   contents of pandoc object
        format  target output format
        meta    document metadata
    '''
    if format != 'latex':
        return

    # Determine what kind of code object this is.
    if key == 'CodeBlock':
        template = Template(
            '\\begin{minted}[$attributes]{$language}\n$contents\n\end{minted}'
        )
        Element = RawBlock
    elif key == 'Code':
        template = Template('\\mintinline[$attributes]{$language}{$contents}')
        Element = RawInline
    else:
        return

    settings = unpack_metadata(meta)

    code = unpack_code(value, settings['language'])

    return [Element(format, template.substitute(code))]


if __name__ == '__main__':
    toJSONFilter(minted)

예제 #15
0
#!/usr/bin/env python

"""

Pandoc filter for assisting with html output

"""

from pandocfilters import toJSONFilter, RawBlock, Div


def latex(x):
    return RawBlock('latex', x)


def latexdivs(key, value, format, meta):
    if key == 'Div':
        [[ident, classes, kvs], contents] = value
        if ["latex","true"] in kvs:
            if format == "latex":
                if ident == "":
                    label = ""
                else:
                    label = '\\label{' + ident + '}'
                return([latex('\\begin{quotation}\\textbf{' + ident + '}')] + contents +
                       [latex('\\end{quotation}')])

if __name__ == "__main__":
    toJSONFilter(latexdivs)
      else:
        highlightLineNumbersArray = []

      if highlightCharacter is None:
        highlightCharacter = "*"

      if shouldTrimWhitespace is None:
        shouldTrimWhitespace = False
      elif shouldTrimWhitespace == "trim":
        shouldTrimWhitespace = True
      else:
        shouldTrimWhitespace = False

      # Use the extension as the code block type
      extension = os.path.splitext(filename)[1].replace(".", "")

      if extension == "hql":
        # Change HiveQL to SQL
        extension = "sql"
      elif extension == "m":
        # Change m to Objective C
        extension = "objectivec"
      elif extension == "h":
        # Change m to Objective C
        extension = "objectivec"

      return CodeBlock(['', [extension], []], processIncludeLine(filename, includeLineNumbersArray, highlightLineNumbersArray, highlightCharacter, shouldTrimWhitespace))

if __name__ == "__main__":
  toJSONFilter(includesource)
예제 #17
0
GITHUB_URL = "https://github.com/cjerdonek/open-rcv/blob/master/"
PYPI_URL = "https://pypi.python.org/pypi/OpenRCV/"

log = logging.getLogger(__file__)


def convert_url(url):
    """Convert URL appearing in a markdown file to a new URL.

    Returns None if URL should remain same.
    """
    parsed_url = urlparse(url)
    log.debug(repr(parsed_url))
    url_path = parsed_url[2]
    if not url_path:
        # Then we assume it is a fragment.
        new_url = urlunparse(parsed_url)
        new_url = urljoin(PYPI_URL, new_url)
        return new_url
    if (not url_path.endswith(".md") and
        url_path != "LICENSE"):
        return None
    # Otherwise, we link back to the original GitHub pages.
    new_url = urlunparse(parsed_url)
    new_url = urljoin(GITHUB_URL, new_url)
    return new_url


if __name__ == "__main__":
    toJSONFilter(init_action(convert_url))
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

##
# Increments all headers by 1 level to a maximum of 6 levels.
#

import os
import sys

from pandocfilters import toJSONFilter, Header

LEVELS = 1
MAX_LEVEL = 6


def inc_header(key, value, format, _):
    if key == 'Header':
        [level, attr, inline] = value
        if level + LEVELS <= MAX_LEVEL:
            level += LEVELS
        return Header(level, attr, inline)


if __name__ == "__main__":
    toJSONFilter(inc_header)
예제 #19
0
#!/usr/bin/env python
from pandocfilters import toJSONFilter, RawInline

"""
Pandoc filter that causes emphasis to be rendered using
the custom macro '\myemph{...}' rather than '\emph{...}'
in latex.  Other output formats are unaffected.
"""

def latex(s):
  return RawInline('latex', s)

def myemph(k, v, f, meta):
  if k == 'Emph' and f == 'latex':
    return [latex('\\myemph{')] + v + [latex('}')]

if __name__ == "__main__":
  toJSONFilter(myemph)
예제 #20
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Pandoc filter to convert divs with class to LaTeX
environments  of the same name.
"""
import sys
from urllib.parse import urlparse

from pandocfilters import toJSONFilter, Link


def internallinks(key, value, format, meta):
    if key == 'Link':
        [attrs, contents, [url, title]] = value
        o = urlparse(url)
        if not o.scheme and not o.netloc and o.fragment:
            url = '#' + o.fragment
            return Link(attrs, contents, (url, title))


if __name__ == "__main__":
    toJSONFilter(internallinks)
	return RawInline('context', s)


def mycite(key, value, fmt, meta):		
	if key == 'Cite' and fmt == 'context':
		
		[[kvs], [contents]] = value
		#kvs = {key: value for key, value in kvs}
		

		if "fig:" in kvs['citationId'] or "tab:" in kvs['citationId']:
			return [context("\in[")]+[context(kvs['citationId'])] + [context(']')]
		else:
			return [context("\\cite[")] +[context(kvs['citationId'])] + [context(']')]
		
			

	# if key == 'Header' and fmt == 'context':
	# 	#warning(key)
	# 	#warning(value)
	# 	[level, contents, kvs] = value
		
	# 	if level == 1 and contents == ['references',[],[]]:
	# 		return RawBlock('context', "\completepublications")

	


if __name__ == "__main__":
	toJSONFilter(mycite)
예제 #22
0
    os.chdir(olddir)
    shutil.copyfile(tmpdir + '/temp.' + filetype, outfile + '.' + filetype)
    shutil.rmtree(tmpdir)


def asy(key, value, format, meta):
    if key == 'CodeBlock':
        [[ident, classes, keyvals], code] = value
        caption = "caption"
        if "asy" in classes:
            outfile = imagedir + '/' + sha1(code)
            if format == "html":
                filetype = "png"
            elif format == "latex":
                filetype = "pdf"
            else:
                filetype = "png"
            src = outfile + '.' + filetype
            if not os.path.isfile(src):
                try:
                    os.mkdir(imagedir)
                    sys.stderr.write('Created directory ' + imagedir + '\n')
                except OSError:
                    pass
                asy2image(code, filetype, outfile)
                sys.stderr.write('Created image ' + src + '\n')
            return Para([Image([], [src, ""])])

if __name__ == "__main__":
    toJSONFilter(asy)
예제 #23
0
    shutil.rmtree(tmpdir)


def tikz(key, value, format, meta):
    # if key == 'RawBlock':
    #     [fmt, code] = value
    #     if fmt == "latex" and re.match("\\\\begin{tikzpicture}", code):
    if key == 'CodeBlock':
        [[ident, classes, keyvals], code] = value
        if "tikz" in classes:
            outfile = imagedir + '/' + sha1(code)
            if format == "html":
                filetype = "png"
            elif format == "latex":
                filetype = "pdf"
            else:
                filetype = "png"
            src = outfile + '.' + filetype
            if not os.path.isfile(src):
                try:
                    os.mkdir(imagedir)
                    sys.stderr.write('Created directory ' + imagedir + '\n')
                except OSError:
                    pass
                tikz2image(code, filetype, outfile)
                sys.stderr.write('Created image ' + src + '\n')
            return Para([Image([], [src, ""])])

if __name__ == "__main__":
    toJSONFilter(tikz)
예제 #24
0
파일: centered.py 프로젝트: ft/spaceman
#!/usr/bin/env python3

import pandocfilters as pf


def latex(s):
    return pf.RawBlock('latex', s)


def mk_centered(k, v, f, m):
    if k == "Para":
        value = pf.stringify(v)
        if value.startswith('[') and value.endswith(']'):
            content = value[1:-1]
            if content == "center":
                return latex(r'\begin{center}')
            elif content == "/center":
                return latex(r'\end{center}')


if __name__ == "__main__":
    pf.toJSONFilter(mk_centered)
예제 #25
0
def configure_logging():
    format_string = "%(name)s: [%(levelname)s] %(message)s"
    logging.basicConfig(format=format_string, level=logging.DEBUG)
    #log.debug("Debug logging enabled.")

def IsNumber(x):
     import re
     if re.match("^\d*.?\d*$", x) == None:
         return False
     return True
    
def behead(key, value, format, meta):

    if format == 'latex':
        if key == 'Str':
            try:
                # if there is no exception this is english text
                value.encode('ascii')
                # Just change non numeric values
                if not IsNumber(value):
                    if value not in ignored_words:
                        return RawInline('latex', '\\lr{'+value+'}')
            except UnicodeEncodeError:
                #log.info( "string is UTF-8")
                pass
    

if __name__ == "__main__":
    configure_logging()
    toJSONFilter(behead)
예제 #26
0
def main():
    toJSONFilter(plantuml)
예제 #27
0
#!/usr/bin/env python

"""
Pandoc filter to allow interpolation of metadata fields
into a document.  %{fields} will be replaced by the field's
value, assuming it is of the type MetaInlines or MetaString.
"""

from pandocfilters import toJSONFilter, attributes, Span, Str
import re

pattern = re.compile('%\{(.*)\}$')


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'])

if __name__ == "__main__":
    toJSONFilter(metavars)
예제 #28
0
파일: filter.py 프로젝트: egh/talks
#!/usr/bin/env python
from pandocfilters import toJSONFilter, CodeBlock, Image, Para, Str
from matplotlib.pyplot import savefig
import uuid
from pylab import rcParams
rcParams['figure.figsize'] = 2, 2

def evalR(key, value, fmt, meta):
    if key == 'CodeBlock' and value[0][1][0] == "python":
        figfile = "%s.png" % (uuid.uuid4())
        exec(value[1])
        savefig(figfile)
        return Para([Image([Str("Output")], [figfile, "fig:"])])

if __name__ == "__main__":
    toJSONFilter(evalR)
예제 #29
0
                math = [alignHtmlMath(x) for x in math]
                if ident != '':
                    label = ['id=\"' + x + '\" ' for x in id]
                else: label = id
                head = [html('<table class=\"' + 
                             ' '.join(classes) + '\" ' + \
                    ' '.join(kvs) + '>' + "\n")]
                tail = [html('</table>' + "\n")]
                body = [html('<tbody>' + "\n")]
                for (i, eq) in enumerate(math):
                    body = body + [html('<tr>' + "\n")] 
                    for sub in [formatHtmlMath(y, ident != '') for y in eq]:
                        body = body + sub
                    if ident != '':                     
                        body = body + [html(' <td ' + label[i] + 
                                        'class=\"eq_number\"> <br>(' + 
                                        eqNumber(id[i]) + ')<br> </td>')]
                    body = body + [html('</tr>' + "\n")]
                body = body + [html('</tbody>' + "\n")]
                return head + body + tail
    if key == 'Span':
        [[ident,classes,kvs], contents] = value
        if 'eq_ref' in classes:
            if format == 'latex':
                return latexInline("(" + "\\ref{" + ident + "})")
            if format == 'html' or format == 'html5':
                return htmlInline("(<a href=#" + ident + ">" + eqNumber(ident) + "</a>)")
        
if __name__ == '__main__':
    toJSONFilter(equation)
예제 #30
0
  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)

if __name__ == "__main__":
  toJSONFilter(behead)

예제 #31
0
#!/usr/bin/python

from pandocfilters import toJSONFilter, stringify, Link

def duck(key, value, format_, meta):
    '''
    If a link is of the form "!STRING", use the !-expression to search
    DuckDuckGo.  So for instance [Fishmans](!w) would search Wikipedia
    for "Fishmans".
    '''
    if key == 'Link':
        [txt, [url, attr]] = value
        if url.startswith("!"):
            url = "http://duckduckgo.com/?q=" + url + " " + stringify(txt)
            return Link(txt, [url, attr])

if __name__ == '__main__':
    toJSONFilter(duck)
예제 #32
0
"""
Pandoc filter to allow interpolation of metadata fields
into a document.  %{fields} will be replaced by the field's
value, assuming it is of the type MetaInlines or MetaString.
"""

from pandocfilters import toJSONFilter, attributes, Span, Str
import re

pattern = re.compile('%\{(.*)\}$')


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:
                return Span(
                    attributes({
                        'class': 'interpolated',
                        'field': field
                    }), result['MetaInlines'])
            elif 'MetaString' in result:
                return Str(result['MetaString'])


if __name__ == "__main__":
    toJSONFilter(metavars)
예제 #33
0
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

from pandocfilters import toJSONFilter, RawBlock, Div


def latex(x):
    return RawBlock('latex', x)

def lois(key, value, format, meta):
    if key == 'Div':
        [[ident, classes, kvs], contents] = value
        if "loi" in classes:
            if format == "latex":
                if ident == "":
                    label = ""
                else:
                    label = '\\label{' + ident + '}'
                return([latex('\\begin{loi}' + label)] + contents +
                       [latex('\\end{loi}')])
            elif format == "html" or format == "html5":
                newcontents = [html('<div class="loi"><blockquote> ' + contents + '</blockquote></div>')]
                return Div([ident, classes, kvs], newcontents)

if __name__ == "__main__":
    toJSONFilter(lois)
예제 #34
0
        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])])


if __name__ == "__main__":
    toJSONFilter(graphviz)
예제 #35
0
      ````{raw=latex}
      \usepackage{pgfpages}
      \pgfpagesuselayout{2 on 1}[a4paper]
      ````
    ...

See <https://github.com/jgm/pandoc/issues/2139>
"""

from pandocfilters import RawInline, RawBlock, toJSONFilter
from pandocattributes import PandocAttributes

raw4code = {'Code': RawInline, 'CodeBlock': RawBlock}


def code2raw(key, val, format, meta):
    if key not in raw4code:
        return None
    attrs = PandocAttributes(val[0], format='pandoc')
    raw = attrs.kvs.get('raw', None)
    if raw:
        # if raw != format:     # but what if we output markdown?
        #     return []
        return raw4code[key](raw, val[-1])
    else:
        return None


if __name__ == "__main__":
    toJSONFilter(code2raw)
예제 #36
0
파일: filter.py 프로젝트: b1592/BSc-Project
#!/usr/bin/env python

"""
Pandoc filter to convert all regular text to uppercase.
Code, link URLs, etc. are not affected.
"""

from pandocfilters import toJSONFilter, Str, RawInline, Code

def latex(s):
  return RawInline('latex', s)

def caps(key, value, format, meta):
  if key == 'Str' and value == "->":
    return latex("$\\rightarrow$")

if __name__ == "__main__":
  toJSONFilter(caps)
예제 #37
0
파일: wpmath.py 프로젝트: dhesse/Blog
from pandocfilters import toJSONFilter, Str, RawBlock
import re
from itertools import islice

def getLines(attrs):
    lines_re = re.match("(\d+)-(\d+)", attrs.get('lines', ""))
    if lines_re:
        return (int(lines_re.group(1)),
                int(lines_re.group(2)) + 1)
    return (0, None)

def wpmath(key, value, format, meta):
    if key == "Math":
        return Str("$LaTeX " + value[1] + "$")
    if key == "CodeBlock":
        language = value[0][1]
        attrs = dict(value[0][2])
        if 'fromFile' in attrs:
            with open(attrs['fromFile']) as codeFile:
                return RawBlock(value[0], '\n'.join(islice(codeFile, *getLines(attrs))))
        #print value
        #return RawBlock(value[0], str(value[1]))

if __name__ == "__main__":
    toJSONFilter(wpmath)
예제 #38
0
FLAG_PAT = re.compile('.*\{(\d+\.?\d?)\}')

def wrapfig(key, val, fmt, meta):
    if key == 'Image':
        attrs, caption, target = val
        if FLAG_PAT.match(stringify(caption)):
            # Strip tag
            size = FLAG_PAT.match(caption[-1]['c']).group(1)
            stripped_caption = caption[:-2]
            if fmt == 'latex':
                latex_begin = r'\begin{wrapfigure}{r}{' + size + 'in}'
                if len(stripped_caption) > 0:
                    latex_fig = r'\centering\includegraphics{' + target[0] \
                                + '}\caption{'
                    latex_end = r'}\end{wrapfigure}'
                    return [RawInline(fmt, latex_begin + latex_fig)] \
                            + stripped_caption + [RawInline(fmt, latex_end)]
                else:
                    latex_fig = r'\centering\includegraphics{' + target[0] \
                                + '}'
                    latex_end = r'\end{wrapfigure}'
                    return [RawInline(fmt, latex_begin + latex_fig)] \
                            + [RawInline(fmt, latex_end)]
            else:
                return Image(attrs, stripped_caption, target)

if __name__ == '__main__':
    toJSONFilter(wrapfig)
    sys.stdout.flush() # Should fix issue #1 (pipe error)
예제 #39
0
    elif "include" in classes:
        caption, typef, keyvals = get_caption(keyvals)
        keyvalDict = dict(keyvals)

        listing = ''
        with open( keyvalDict['src'], 'r' ) as f:
            listing = f.read() 

        lang = keyvalDict.get( 'lang', 'python')
        code = 'Failed to find any listing.'
        if 'start' in keyvalDict:
            # Assume that both start and end are line numbers.
            start = int( keyvalDict['start'] )
            end = int( keyvalDict['end'] )
            code = '\n'.join( listing.split('\n')[start:end] )
        elif 'pat' in keyvalDict:
            pat = r'%s' % keyvalDict['pat']
            print1( pat )
            m = re.search( pat, listing, re.DOTALL )
            if m:
                code = m.group(0)
            else:
                code = "Pattern '%s' not found in '%s'" % (pat, keyvalDict['src'])
        else:
            code = 'No listing found.'
        return CodeBlock([ident, [], keyvals], code)

if __name__ == "__main__":
    toJSONFilter( codeblocks )
예제 #40
0
def main():
    environment = Environment()
    pdf.toJSONFilter(environment.convert)
Pandoc filter that causes everything between
'<!-- BEGIN SOLUTION -->' and '<!-- END SOLUTION -->'
to be ignored.  The comment lines must appear on
lines by themselves, with blank lines surrounding
them.
"""

from pandocfilters import toJSONFilter
import re

incomment = False


def comment(k, v, fmt, meta):
    global incomment
    if k in ['RawBlock', 'RawInline']:
        fmt, s = v
        if fmt == "html":
            if re.search("<!-- BEGIN SOLUTION -->", s):
                incomment = True
                return []
            elif re.search("<!-- END SOLUTION -->", s):
                incomment = False
                return []
    if incomment:
        return []  # suppress anything in a comment


if __name__ == "__main__":
    toJSONFilter(comment)
예제 #42
0
def svg_to_any(key, value, fmt, meta):
    if key == "Image":
        if len(value) == 2:
            # before pandoc 1.16
            alt, [src, title] = value
            attrs = None
        else:
            attrs, alt, [src, title] = value
        mimet, _ = mimetypes.guess_type(src)
        option = fmt_to_option.get(fmt)
        if mimet == "image/svg+xml" and option:
            base_name, _ = os.path.splitext(src)
            eps_name = base_name + "." + option
            try:
                mtime = os.path.getmtime(eps_name)
            except OSError:
                mtime = -1
            if mtime < os.path.getmtime(src):
                cmd_line = ["cairosvg", "-f", option, "-o", eps_name, src]
                sys.stderr.write("Running %s\n" % " ".join(cmd_line))
                subprocess.call(cmd_line, stdout=sys.stderr.fileno())
            if attrs:
                return Image(attrs, alt, [eps_name, title])
            else:
                return Image(alt, [eps_name, title])


if __name__ == "__main__":
    toJSONFilter(svg_to_any)
예제 #43
0
            # a blockquote is just a list of blocks, so it can be
            # passed directly to Div, which expects Div(attr, blocks)
            return pf.Div((id, classes, kvs), [panel_header, panel_body])


if __name__ == '__main__':
    # pandocfilters.toJSONFilter is a convenience method that
    # makes a command line json filter from a given function.
    # JSON emitted from pandoc is read from stdin. The JSON tree is
    # walked, with the function being applied to each element in the
    # tree.
    #
    # The function passed to to JSONFilter must accept (key, value,
    # format, metadata) as arguments:
    #
    # key - element type (e.g. 'Str', 'Header')
    # value - element contents
    # format - destination format
    # metadata - document metadata
    #
    # The function return values determine what happens to the
    # element:
    #     returns None: the element is unmodified;
    #     returns []: delete the element
    #     otherwise: replace the element with the return value
    #
    # The JSON is then output to stdout, where it can be consumed by
    # pandoc.
    pf.toJSONFilter(blockquote2div)
예제 #44
0
            block("\placetable[here][%s]{%s}{" % (strCap[0][0], strCap[0][1])))
        out.append(block("\\bTABLE"))

        if not empty(head):
            out.append(
                block(
                    "\setupTABLE[r][1][background=color,backgroundcolor=gray,style={\\tfa}]"
                ))
            out.append(block("\\bTR"))
            for h in head:
                out.append(block("\\bTH %s \\eTH" % (stringify(h))))
            out.append(block("\\eTR"))
        else:
            out.append(
                block(
                    "\setupTABLE[r][1][background=color,backgroundcolor=white,style={\\tfx}]"
                ))

        for row in text:
            out.append(block("\\bTR"))
            for cell in row:
                out.append(block("\\bTD %s \\eTD" % stringify(cell)))
            out.append(block("\\eTR"))
        out.append(block("\\eTABLE}"))

        return out


if __name__ == "__main__":
    toJSONFilter(crossreference)
예제 #45
0
        meta:    document metadata
    """

    if format == "html":
        if key == 'CodeBlock':
            body, language, params, source_file = unpack(value, meta)
            if language is None:
                return

            if source_file is None:
                html = '<pre><code class="language-%s">%s</code></pre>' % (language, body)
                return [RawBlock(format, html)]

            else:
                content = r'\inputminted[' + params + ']{' + language + '}{' + source_file + '}'
                return [RawBlock(format, content)]

        elif key == 'Code':
            body, language, params, source_file = unpack(value, meta)

            if language is None:
                return

            html = '<code class="language-%s">%s</code>' % (language, body)
            return [RawInline(format, html)]

if __name__ == '__main__':
    # import sys
    # sys.stdin = open("test.json")
    toJSONFilter(prism)
예제 #46
0
파일: plantuml.py 프로젝트: ccat3z/notebook
g = {'output': None}


def get_image(src):
    outfilename = str(get_filename4code('temp-plantuml', src, 'png'))
    file = open(outfilename, 'w')
    p = subprocess.Popen(['plantuml', '-p', '-tpng'], stdin=subprocess.PIPE, stdout=file)
    p.stdin.write(src.encode('UTF-8'))
    p.communicate()
    p.stdin.close()
    return outfilename


def index(key, value, format, meta):
    if key == 'CodeBlock':
        [[ident, classes, kvs], contents] = value
        caption, typef, kvs = get_caption(kvs)
        if 'plantuml' in classes and 'embed' in classes:
            return Para([
                Image(
                    [ident, [], kvs],
                    caption,
                    [get_image(contents), typef]
                )
            ])


if __name__ == "__main__":
    toJSONFilter(index)
예제 #47
0
#         if len(value) == 2:
#             url = Str( value[-1][0] )
#             name = Str( ' '.join([
#             return Link([' '.join([ walk( val, deconcat, format, meta ) for val in value ])], [ url ] )
#     if key == 'Str':
#         return Str( value )
#     elif key == 'Space':
#         return Space([ ])


def pchecko(key, value, format, meta):
    logging.debug('HUUU %s: %s' % (key, value))


def imStuff(key, value, format, meta):
    if key == 'Image':
        logging.debug('%s: %s, %d' % (key, value[-1], len(value)))
        # return walk(value[-1], pchecko, format, meta )
        #[[ident, classes, kvs], contents] = value
        #logging.debug('CLASSES: %s' % classes )
        caption, location = value
        # now concatenate the content
        logging.debug('%s: content = %s, %d' % (key, caption, len(caption)))
        return Image([Str("hello")], [location[0], location[1]])


if __name__ == '__main__':
    logging.basicConfig(filename='removewidths_filter.log',
                        level=logging.DEBUG)
    toJSONFilter(imStuff)
예제 #48
0
파일: math.py 프로젝트: sthan41/R_intro
#!/usr/bin/env python

from pandocfilters import toJSONFilter, RawInline, stringify
import re

align = re.compile("\\\\begin{align}")


def math(k, v, f, meta):
    if k == 'Math' and f == 'latex' and re.search(align, v[1]):
        return RawInline('latex', v[1])


if __name__ == "__main__":
    toJSONFilter(math)
    "beamer": ("--export-pdf", "pdf"),
    # because of IE
    "html": ("--export-png", "png")
}


def svg_to_any(key, value, fmt, meta):
    if key == 'Image':
        attrs, alt, [src, title] = value
        mimet, _ = mimetypes.guess_type(src)
        option = fmt_to_option.get(fmt, ("--export-pdf", "pdf"))
        if mimet == 'image/svg+xml' and option:
            base_name, _ = os.path.splitext(src)
            eps_name = base_name + "." + option[1]
            eps_name = eps_name.replace("%20", "")            
            src = src.replace("%20", " ")
            try:
                mtime = os.path.getmtime(eps_name)
            except OSError:
                mtime = -1
            if mtime < os.path.getmtime(src):
                cmd_line = ['inkscape', option[0], eps_name, src]
                sys.stderr.write("Running %s\n" % " ".join(cmd_line))
                subprocess.call(cmd_line, stdout=sys.stderr.fileno())
            return Image(attrs, alt, [eps_name.replace("%20", " "), title])

if __name__ == "__main__":
    toJSONFilter(svg_to_any)


예제 #50
0
#!/usr/bin/env python3

from pandocfilters import toJSONFilter, Math, Para

"""Pandoc filter to convert gitlab flavored markdown to pandoc flavored markdown"""


def gitlab_markdown(key, value, format, meta):
    if key == "CodeBlock":
        [[identification, classes, keyvals], code] = value
        if classes[0] == "math":
            fmt = {'t': 'DisplayMath',
                   'c': []}
            return Para([Math(fmt, code)])

    elif key == "Math":
        [fmt, code] = value
        if isinstance(fmt, dict) and 't' in fmt.keys():
            if fmt['t'] == "InlineMath":
                return Math(fmt, code.strip('`'))


if __name__ == "__main__":
    toJSONFilter(gitlab_markdown)
예제 #51
0
        result.extend(para)
        result[-1] = inlatex(r' \\ \hline' '\n')
    return pf.Para(result)


def do_filter(k, v, f, m):
    if k == "Table":
        # Ensure every alignment characters is surrounded by a pipes.
        # Get the string of the alignment characters
        # and split into an array for every characters.
        split_alignment = [c for c in tbl_alignment(v[1])]
        # Join this list into a single string with pipe symbols
        # between them, plus pipes at start and end.
        # This results in a boxed table.
        new_alignment = "|" + "|".join(split_alignment) + "|"
        return [
            latex(r'\begin{table}[h]'),
            latex(r'\centering'),
            latex(r'\begin{tabular}{%s} \hline' % new_alignment),
            tbl_headers(v[3]),
            tbl_contents(v[4]),
            latex(r'\end{tabular}'),
            # Put the caption after the tabular so it appears under table.
            tbl_caption(v[0]),
            latex(r'\end{table}')
        ]


if __name__ == "__main__":
    pf.toJSONFilter(do_filter)
예제 #52
0
#!/usr/bin/env python

"""
Pandoc filters to customise the HTML / LaTeX output when parsing
documents. To figure out what elements are and how to edit them
run pandoc -f markdown -t json project_overview.md and have a look
at the (very very long) JSON output.
"""

from pandocfilters import toJSONFilter, Image, Str, RawInline


# Replace [tick] or [cross] with different icons in PDF and HTML
def tick_cross_images (key, value, format, meta):
    if key == 'Str' and value == '[tick]':
        if format == "latex":
            return RawInline('latex', r'~\tickmark~~')
        else:
            return RawInline('html', r'<span class="icon_tick">&#10004;</span> ')
    
    if key == 'Str' and value == '[cross]':
        if format == "latex":
            return RawInline('latex', r'~\crossmark~~~')
        else:
            return RawInline('html', r'<span class="icon_cross">&#10008;</span> ')


if __name__ == "__main__":
  toJSONFilter(tick_cross_images)
예제 #53
0
                    code = doc.read().split('%%\n')[1]
                return [
                    Image([], [
                        png(contents, latexsnippet('\\gregorioscore', kvs)), ""
                    ])
                ]
    elif key == 'CodeBlock':
        [[ident, classes, kvs], contents] = value
        kvs = {key: value for key, value in kvs}
        if "gabc" in classes:
            if fmt == "latex":
                if ident == "":
                    label = ""
                else:
                    label = '\\label{' + ident + '}'
                return [
                    latexblock("\n\\smallskip\n{%\n" +
                               latexsnippet('\\gabcsnippet{' + contents +
                                            '}', kvs) + "%\n}" + label)
                ]
            else:
                return Para([
                    Image([], [
                        png(contents, latexsnippet('\\gabcsnippet', kvs)), ""
                    ])
                ])


if __name__ == "__main__":
    toJSONFilter(gabc)
예제 #54
0
    if format == 'latex':

        if key == 'Image':
            options = {key: val for key, val in value[0][2]}
            image_name = value[2][0]
            width = float(options.get("w", .9))

            caption = None
            if len(value[1]) > 0:
                capt = value[1]
                caption = ""
                for part in capt:
                    if part["t"] == "Str":
                        caption += part["c"]
                    elif part["t"] == "Space":
                        caption += " "

            content = center_image(image_name, caption, width)
            return [RawInline(format, content)]

    if key == 'Header' and len(value[1][2]) > 0:
        options = {key: val for key, val in value[1][2]}
        if "magic" in options and options["magic"] in MAGIC_OPTIONS[format]:
            magic_func = MAGIC_OPTIONS[format][options["magic"]]
            return magic_func(format, options)


if __name__ == '__main__':
    # sys.stdin = open("test.json") # testing only
    toJSONFilter(parse)
예제 #55
0
#!/usr/bin/env python
"""
Pandoc filter that:
-  removes backticks within InlineMath contents
"""

from pandocfilters import toJSONFilter, Math
import re

STRIP_BACKTICKS_RE = re.compile(r'^`(?P<contents>.*)`$', re.MULTILINE |
                                re.UNICODE | re.DOTALL)


def cleanup(k, v, fmt, meta):
    if k in ['Math']:
        if fmt in ['latex', 'beamer', 'json', 'html5']:
            math_type = v[0].get('t', None)
            if math_type in [u'InlineMath'] and len(v) > 1:
                solution_match = STRIP_BACKTICKS_RE.search(v[1])
                if solution_match:
                    return Math(v[0], solution_match.group('contents'))


if __name__ == "__main__":
    toJSONFilter(cleanup)
예제 #56
0
                    else:
                        c.append(a)
                if found:
                    label = stringify_maths(b).split(" ")[0].lower()
                    lower = ext.lower()
                    if last_precedent_header:
                        return [latex(r"\begin{" + lower + "}[" + stringify_maths(b) +
                        "]"), pf.Para(c), latex(r"\label{" + label + "}"),
                        latex(r"\end{" + lower + "}")]
                    else:
                        return [latex(r"\vspace{3mm}"), latex(r"\begin{" + lower + "}[" + stringify_maths(b) +
                        "]"), pf.Para(c), latex(r"\label{" + label + "}"),
                        latex(r"\end{" + lower + "}")]

        t = deal_extension("Theorem")
        if t == None:
            t = deal_extension("Lemma")
            if t == None:
                t = deal_extension("Property")
                if t == None:
                    return deal_extension("Definition")
                else:
                    return t
            else:
                return t
        else:
            return t

if __name__ == "__main__":
    pf.toJSONFilter(mk_columns)
예제 #57
0
                    Image(['', [], []],
                        [],
                        [png(code, kvs), ""]
                    )
                ]
    if key == 'CodeBlock':
        [[ident, classes, kvs], code] = value
        kvs = {key: value for key, value in kvs}
        if "ly" in classes:
            kvs = calc_params(kvs, meta)
            if fmt == "latex":
                if ident == "":
                    label = ""
                else:
                    label = '\\label{' + ident + '}'
                return latexblock(
                    '\\lily[staffsize=%s]{%s}' % (kvs['staffsize'], code) +
                    label
                )
            else:
                return Para([
                    Image(
                        ['', [], []],
                        [],
                        [png(code, kvs), ""]
                    )
                ])

if __name__ == "__main__":
    toJSONFilter(lily)
예제 #58
0
def latex(s):
    return pf.RawBlock('latex', s)


def mk_columns(k, v, f, m):
    if k == "Para":
        value = pf.stringify(v)
        if value.startswith('[') and value.endswith(']'):
            content = value[1:-1]
            if content == "columns":
                if f == "beamer":
                    return latex(r'\begin{columns}')
                elif f == "latex":
                    return latex(r'\begin{multicols}{2}')
            elif content == "/columns":
                if f == "beamer":
                    return latex(r'\end{columns}')
                elif f == "latex":
                    return latex(r'\end{multicols}')
            elif content.startswith("column="):
                if f == "beamer":
                    return latex(r'\column{%s\textwidth}' % content[7:])
                elif f == "latex":
                    return latex(r'\columnbreak')


if __name__ == "__main__":

    pf.toJSONFilter(mk_columns)
예제 #59
0
파일: git-diff.py 프로젝트: oncletom/stylo
                    folder = el[1]

                if "objects" in el or "object" in el:
                    objdiv = "--"
                    obj = "%s %s" % (obj, el[1])

                if "diffoptions" == el[0]:
                    options = el[1]

            command_string = "git -C %s diff %s %s %s %s" % (
                folder, options, commra, objdiv, obj)
            out = ""
            try:
                l = list(filter(None, command_string.split(" ")))
                out = subprocess.check_output(l)
            except subprocess.CalledProcessError as err:
                return None

            if out != None or out != "":
                out = out.decode("utf-8")
                return [
                    CodeBlock([ident, classes, keyvals], out),
                    CodeBlock([ident, classes, keyvals], contents)
                ]
            else:
                return None


if __name__ == "__main__":
    toJSONFilter(gitdiff)