Skip to content

Commit

Permalink
Separate equation into its own file. User should not have to fire up …
Browse files Browse the repository at this point in the history
…a onedoc class only to convert functions to latex/word snippets
  • Loading branch information
Krande committed Feb 1, 2022
1 parent a287d80 commit 4710ed5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 51 deletions.
3 changes: 2 additions & 1 deletion src/paradoc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .common import MY_DOCX_TMPL, MY_DOCX_TMPL_BLANK
from .document import OneDoc
from .equations import Equation

__all__ = [MY_DOCX_TMPL, MY_DOCX_TMPL_BLANK, OneDoc]
__all__ = [MY_DOCX_TMPL, MY_DOCX_TMPL_BLANK, OneDoc, Equation]
41 changes: 1 addition & 40 deletions src/paradoc/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pathlib
import re
from dataclasses import dataclass, field
from typing import Callable, List, Union
from typing import List, Union

import pandas as pd

Expand Down Expand Up @@ -72,45 +72,6 @@ class Figure:
docx_instances: List[object] = field(default_factory=list)


@dataclass
class Equation:
name: str
func: Callable
custom_eq_str_compiler: Callable = None
add_link: bool = True
include_python_code: bool = False
md_instances: List[MarkDownFile] = field(default_factory=list)
docx_instances: List[object] = field(default_factory=list)

def to_latex(self, print_latex=False, print_formula=False, flags=None):
if self.custom_eq_str_compiler is not None:
return self.custom_eq_str_compiler(self.func)

from inspect import getsource, getsourcelines

import pytexit

lines = getsourcelines(self.func)
eq_latex = ""
matches = ("def", "return", '"')
dots = 0
for line in lines[0]:
if any(x in line for x in matches):
dots += line.count('"')
dots += line.count("'")
continue
if dots >= 6 or dots == 0:
eq_latex += pytexit.py2tex(line, print_latex=print_latex, print_formula=print_formula) + "\n"
eq_str = eq_latex

if self.add_link:
eq_str += f"{{#eq:{self.name}}}"

if self.include_python_code:
eq_str = f"\n\n```python\n{getsource(self.func)}\n```\n\n" + eq_str
return eq_str


@dataclass
class DocXFormat:
pg_font: str = "Arial"
Expand Down
11 changes: 2 additions & 9 deletions src/paradoc/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@

import pandas as pd

from .common import (
DocXFormat,
Equation,
ExportFormats,
Figure,
MarkDownFile,
Table,
TableFormat,
)
from .common import DocXFormat, ExportFormats, Figure, MarkDownFile, Table, TableFormat
from .equations import Equation
from .exceptions import LatexNotInstalled
from .utils import get_list_of_files

Expand Down
45 changes: 45 additions & 0 deletions src/paradoc/equations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import annotations

from dataclasses import dataclass, field
from typing import Callable, List

from .common import MarkDownFile


@dataclass
class Equation:
name: str
func: Callable
custom_eq_str_compiler: Callable = None
add_link: bool = True
include_python_code: bool = False
md_instances: List[MarkDownFile] = field(default_factory=list)
docx_instances: List[object] = field(default_factory=list)

def to_latex(self, print_latex=False, print_formula=False, flags=None):
if self.custom_eq_str_compiler is not None:
return self.custom_eq_str_compiler(self.func)

from inspect import getsource, getsourcelines

import pytexit

lines = getsourcelines(self.func)
eq_latex = ""
matches = ("def", "return", '"')
dots = 0
for line in lines[0]:
if any(x in line for x in matches):
dots += line.count('"')
dots += line.count("'")
continue
if dots >= 6 or dots == 0:
eq_latex += pytexit.py2tex(line, print_latex=print_latex, print_formula=print_formula) + "\n"
eq_str = eq_latex

if self.add_link:
eq_str += f"{{#eq:{self.name}}}"

if self.include_python_code:
eq_str = f"\n\n```python\n{getsource(self.func)}\n```\n\n" + eq_str
return eq_str
3 changes: 2 additions & 1 deletion src/paradoc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import pypandoc

from .common import Equation, MarkDownFile, Table
from .common import MarkDownFile, Table
from .equations import Equation


def func_to_eq(func):
Expand Down

0 comments on commit 4710ed5

Please sign in to comment.