-
Notifications
You must be signed in to change notification settings - Fork 127
add a latex macro to convert Tex code to <img> tag using dataURI. #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dzhuang
wants to merge
5
commits into
inducer:main
Choose a base branch
from
dzhuang:latex2img
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7eb9f9c
add a latex macro to convert Tex code to <img> tag using dataURI.
dzhuang c7fb203
Merge remote-tracking branch 'remotes/upstream/master' into latex2img
dzhuang ead7cb1
Merge remote-tracking branch 'remotes/upstream/master' into latex2img
dzhuang edb7ace
Merge branch 'master' of git://github.com/inducer/relate into latex2img
dzhuang 5ccf9a5
use mongodb to store the results.
dzhuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| from __future__ import division | ||
|
|
||
| __copyright__ = "Copyright (C) 2016 Dong Zhuang, Andreas Kloeckner" | ||
|
|
||
| __license__ = """ | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| """ | ||
|
|
||
| from django.core.checks import register, Tags as DjangoTags | ||
| from django.conf import settings | ||
|
|
||
| from course.latex.utils import get_all_indirect_subclasses | ||
| from course.latex.converter import CommandBase | ||
|
|
||
|
|
||
| class Tags(DjangoTags): | ||
| relate_course_tag = 'relate_course_tag' | ||
|
|
||
|
|
||
| @register(Tags.relate_course_tag, deploy=True) | ||
| def latex2image_bin_check(app_configs, **kwargs): | ||
| """ | ||
| Check if all tex compiler and image converter | ||
| are correctly configured, if latex utility is | ||
| enabled. | ||
| """ | ||
| if not getattr(settings, "RELATE_LATEX_TO_IMAGE_ENABLED", False): | ||
| return [] | ||
| klass = get_all_indirect_subclasses(CommandBase) | ||
| instance_list = [cls() for cls in klass] | ||
| errors = [] | ||
| for instance in instance_list: | ||
| error = instance.check() | ||
| if error: | ||
| errors.append(error) | ||
| return errors | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| from __future__ import division | ||
|
|
||
| __copyright__ = "Copyright (C) 2016 Dong Zhuang, Andreas Kloeckner" | ||
|
|
||
| __license__ = """ | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in | ||
| all copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. | ||
| """ | ||
|
|
||
| import six | ||
| import re | ||
|
|
||
| from django.utils.translation import ugettext as _ | ||
|
|
||
| from course.latex.converter import get_tex2img_class | ||
| from course.latex.latex import TexDoc | ||
| from course.latex.utils import ( | ||
| replace_latex_space_seperator, strip_spaces) | ||
|
|
||
| TIKZ_PGF_RE = re.compile(r"\\begin\{(?:tikzpicture|pgfpicture)\}") | ||
| DEFAULT_IMG_HTML_CLASS = "img-responsive" | ||
|
|
||
| # {{{ mypy | ||
|
|
||
| if False: | ||
| from typing import Text, Any, Optional # noqa | ||
|
|
||
| # }}} | ||
|
|
||
|
|
||
| def tex_to_img_tag(tex_source, *args, **kwargs): | ||
| # type: (Text, *Any, **Any) -> Optional[Text] | ||
| '''Convert LaTex to IMG tag''' | ||
|
|
||
| compiler = kwargs.get("compiler", None) | ||
| if not compiler: | ||
| raise ValueError(_("'compiler' must be specified.")) | ||
|
|
||
| image_format = kwargs.get("image_format", "") | ||
| if not image_format: | ||
| raise ValueError(_("'image_format' must be specified.")) | ||
|
|
||
| tex_filename = kwargs.get("tex_filename", None) | ||
| tex_preamble = kwargs.get("tex_preamble", "") | ||
| tex_preamble_extra = kwargs.get("tex_preamble_extra", "") | ||
|
|
||
| force_regenerate = kwargs.get("force_regenerate", False) | ||
| html_class_extra = kwargs.get("html_class_extra", "") | ||
| empty_pagestyle = kwargs.get("empty_pagestyle", True) | ||
| alt = kwargs.get("alt", None) | ||
|
|
||
| # remove spaces added to latex code in jinja template. | ||
| tex_source = replace_latex_space_seperator( | ||
| strip_spaces(tex_source, allow_single_empty_line=True)) | ||
| tex_preamble = replace_latex_space_seperator( | ||
| strip_spaces(tex_preamble, allow_single_empty_line=True)) | ||
| tex_preamble_extra = replace_latex_space_seperator( | ||
| strip_spaces(tex_preamble_extra, | ||
| allow_single_empty_line=True)) | ||
|
|
||
| if html_class_extra: | ||
| if isinstance(html_class_extra, list): | ||
| html_class_extra = " ".join(html_class_extra) | ||
| elif not isinstance(html_class_extra, six.string_types): | ||
| raise ValueError( | ||
| _('"html_class_extra" must be a string or a list')) | ||
| html_class = "%s %s" % (DEFAULT_IMG_HTML_CLASS, html_class_extra) | ||
| else: | ||
| html_class = DEFAULT_IMG_HTML_CLASS | ||
|
|
||
| texdoc = TexDoc( | ||
| tex_source, preamble=tex_preamble, | ||
| preamble_extra=tex_preamble_extra, empty_pagestyle=empty_pagestyle) | ||
|
|
||
| # empty document | ||
| if not texdoc.document.strip(): | ||
| return "" | ||
|
|
||
| if (compiler == "latex" | ||
| and image_format == "png" | ||
| and | ||
| re.search(TIKZ_PGF_RE, tex_source)): | ||
| image_format = "svg" | ||
|
|
||
| assert isinstance(compiler, six.text_type) | ||
|
|
||
| tex2img_class = get_tex2img_class(compiler, image_format) # type: ignore | ||
|
|
||
| if not alt: | ||
| alt = texdoc.document | ||
|
|
||
| if alt: | ||
| alt = "alt='%s'" % alt.strip().replace("\n", "") | ||
|
|
||
| latex2img = tex2img_class( | ||
| tex_source=texdoc.as_latex(), | ||
| tex_filename=tex_filename, | ||
| ) | ||
|
|
||
| return ( | ||
| "<img src='%(src)s' " | ||
| "class='%(html_class)s' %(alt)s>" | ||
| % { | ||
| "src": latex2img.get_data_uri_cached(force_regenerate), | ||
| "html_class": html_class, | ||
| "alt": alt, | ||
| }) | ||
|
|
||
| # vim: foldmethod=marker |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be duplicated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not saying it's wrong, in fact I don't know--the duplication just looks fishy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. I'll correct it.