Git Product home page Git Product logo

markdown2latex's Introduction

python-markdown extension to output LaTeX (rather than html) - i.e. to provide support for converting markdown to LaTeX.

Authored by Rufus Pollock: http://rufuspollock.org/

Reworked by Julian Wulfheide ([email protected]) and Pedro Gaudêncio ([email protected])

Usage:

1. Command Line. A script entitled markdown2latex.py is automatically installed. For details of usage see help::

$ markdown2latex.py -h

2. As a python-markdown extension::

>>> import markdown
>>> md = markdown.Markdown(None, extensions=['latex'])
>>> # text is input string ...
>>> latex_out = md.convert(text)

3. Directly as a module (slight inversion of std markdown extension setup)::

>>> import markdown
>>> import mdx_latex
>>> md = markdown.Markdown()
>>> latex_mdx = mdx_latex.LaTeXExtension()
>>> latex_mdx.extendMarkdown(md, markdown.__dict__)
>>> out = md.convert(text)

History

Version: 1.0 (November 15, 2006)

  • First working version (compatible with markdown 1.5)
  • Includes support for tables

Version: 1.1 (January 17, 2007)

  • Support for verbatim and images

Version: 1.2 (June 2008)

  • Refactor as an extension.
  • Make into a proper python/setuptools package.
  • Tested with markdown 1.7 but should work with 1.6 and (possibly) 1.5 (though pre/post processor stuff not as worked out there)

Version 1.3: (July 2008)

  • Improvements to image output (width)

Version 1.3.1: (August 2009)

  • Tiny bugfix to remove duplicate keyword argument and set zip_safe=False
  • Add [width=\textwidth] by default for included images

Version 2.0: (June 2011)

  • PEP8 cleanup
  • Major rework since this was broken by new Python-Markdown releases

Version 2.1: (August 2013)

  • Add handler for non locally referenced images, hyperlinks and horizontal rules
  • Update math delimiters

markdown2latex's People

Contributors

aegges avatar jjk96 avatar pedrogaudencio avatar rufuspollock avatar sajozsattila avatar scientes avatar thht avatar yogeshg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

markdown2latex's Issues

Tabularx environments: Hash/Pound-Symbol of <url> is only escaped text-part of \href{}{}

Converting an url like
<https://tex.stackexchange.com/questions/44738/hash-tag-in-href-causes-error/44750#44750>
results in

<root>

\href{https://tex.stackexchange.com/questions/44738/hash-tag-in-href-causes-error/44750#44750}{https://tex.stackexchange.com/questions/44738/hash-tag-in-href-causes-error/44750\#44750}


</root>

This is working for normal purposes. However, if you are in an tabularx environment, the #-symbol needs to be escaped everywhere. Even it is escaped, the href command will open the correct url, when you click on it in the compiled latex output. For that reason the proposed solution should be. where the # is also escaped in the link-part of the href command.

<root>

\href{https://tex.stackexchange.com/questions/44738/hash-tag-in-href-causes-error/44750\#44750}{https://tex.stackexchange.com/questions/44738/hash-tag-in-href-causes-error/44750\#44750}


</root>

Multiple link occurrences in text are always replaced with the first one

import markdown
import mdx_latex
md = markdown.Markdown()
latex_mdx = mdx_latex.LaTeXExtension()
latex_mdx.extendMarkdown(md, markdown.__dict__)
out = md.convert("[The first url](https://www.google.de). Text in between. [The second url](https://www.bing.com)")
print(out)

leads to

<root>

\href{https://www.google.de}{The first url}. Text in between. \href{https://www.google.de}{The first url}


</root>

however, it should be converted to

<root>

\href{https://www.google.de}{The first url}. Text in between. \href{https://www.bing.com}{The second url}


</root>

Problem is in https://github.com/rufuspollock/markdown2latex/blob/master/mdx_latex.py#L534 as the one regex replaces all matches with the identical text.

Doesn't work together with markdown-3.0

With markdown-3.0 inlinePatterns seems not to be iterable anymore with iteritems():

$ pip install markdown
Collecting markdown
  Using cached https://files.pythonhosted.org/packages/7a/fd/e22357c299e93c0bc11ec8ba54e79f98dd568e09adfe9b39d6852c744938/Markdown-3.0-py2.py3-none-any.whl
Installing collected packages: markdown
Successfully installed markdown-3.0
~/coding/markdown2latex$ ./mdx_latex.py recommendations.md
Traceback (most recent call last):
  File "./mdx_latex.py", line 700, in <module>
    main()
  File "./mdx_latex.py", line 690, in main
    mkdn2latex.extendMarkdown(md, markdown.__dict__)
  File "./mdx_latex.py", line 133, in extendMarkdown
    for key, pat in self.md.inlinePatterns.iteritems():
AttributeError: 'Registry' object has no attribute 'iteritems'

Error on accented character

Hi,

I have an error with a file containing accented characters. (with commit version f803b09)

Here is the trace:

Traceback (most recent call last):
  File "mdx_latex.py", line 700, in <module>
    main()
  File "mdx_latex.py", line 691, in main
    out = md.convert(infile.read())
  File "/usr/lib/python2.7/dist-packages/markdown/__init__.py", line 333, in convert
    source = util.text_type(source)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 33: ordinal not in range(128). -- Note: Markdown only accepts unicode input!

My file is in UTF-8 and it contains the following text:

# My title

My accented content, é.

Thanks,
Clément

Error in mdx_latex.py, line 169: 'Registry' object does not support item assignment

When I try to run:

import markdown
import mdx_latex

# text is input string ...
md = markdown.Markdown()
latex_mdx = mdx_latex.LaTeXExtension()
latex_mdx.extendMarkdown(md, markdown.__dict__) # <--- line that triggers the error

It gives me the following error:

Exception has occurred: TypeError
'Registry' object does not support item assignment
  File "<path>\markdown2latex\mdx_latex.py", line 169, in extendMarkdown
    md.treeprocessors['latex'] = latex_tp
  File "<path>\markdown2latex\markdownToLatex.py", line 7, in <module>
    latex_mdx.extendMarkdown(md, markdown.__dict__)

Replace \href with \url if possible

The current Link2Latex implementation replaces html hrefs of the form <a href=URL>DESCRIPTION</a> always with \href{DESCRIPTION}{URL}. However, if description and url are identical e.g. because of the original markdown <URL> or [URL](URL), latex's \href{URL}{URL} can be simlified with \url{URL}.

License unclear

The only place I could find a reference to a license is on setup.py (which is also read by PyPI). However, it says MIT and BSD and PyPI renders it as BSD License (MIT) which I believe it's not "right".

Doesn't work with several important features

As of today, the support for the many important features is missing:

Links are causing errors

Code

[a](https://google.com)

Error

Python log
Traceback (most recent call last):
  File "/usr/lib/python3.8/sre_parse.py", line 1039, in parse_template
    this = chr(ESCAPES[this][1])
KeyError: '\\h'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "modules/ssl-websites/main.py", line 91, in <module>
    sec.append(load_markdown(f'{MODULE_NAME}/a.md'))
  File "/home/dzordzu/school/cyber/lab-official/lib/loader.py", line 68, in load_markdown
    result = md2latex(result)
  File "/home/dzordzu/school/cyber/lab-official/lib/markdown.py", line 11, in md2latex
    md_text = md.convert(text)
  File "/usr/lib/python3.8/site-packages/markdown/core.py", line 291, in convert
    output = pp.run(output)
  File "/usr/lib/python3.8/site-packages/markdown2latex-2.1-py3.8.egg/mdx_latex.py", line 508, in run
    latex_link = re.sub(r'<a[^>]*>([^<]+)</a>',
  File "/usr/lib/python3.8/re.py", line 210, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "/usr/lib/python3.8/re.py", line 327, in _subx
    template = _compile_repl(template, pattern)
  File "/usr/lib/python3.8/re.py", line 318, in _compile_repl
    return sre_parse.parse_template(repl, pattern)
  File "/usr/lib/python3.8/sre_parse.py", line 1042, in parse_template
    raise s.error('bad escape %s' % this, len(this))
re.error: bad escape \h at position 0

Tables are not parsed

Code

### TEST

| A      | B      |
|--------|--------|
| asad a | dsdfda |

Result

image

Images are not shown

Code

![ALT](https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png)

Result

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.