Text snippet created by the formatters.
| 644 | } |
| 645 | |
| 646 | class Snippet: |
| 647 | """ |
| 648 | Text snippet created by the formatters. |
| 649 | """ |
| 650 | def __init__(self, snippet: str = "", format: None | str = None, mode="w") -> None: |
| 651 | self._snippet = snippet |
| 652 | self.mode = mode |
| 653 | if format is None: |
| 654 | self._format = "raw" |
| 655 | self._prefix, self._suffix = _FORMATS[self._format] |
| 656 | else: |
| 657 | try: |
| 658 | self._prefix, self._suffix = _FORMATS[format] |
| 659 | self._format = format |
| 660 | except KeyError: |
| 661 | raise KeyError(f"Unknown formatting: {format}.") |
| 662 | |
| 663 | @property |
| 664 | def snippet(self): |
| 665 | """ |
| 666 | Will be set by the formatter method. |
| 667 | """ |
| 668 | return self._snippet |
| 669 | |
| 670 | @property |
| 671 | def format(self): |
| 672 | """ |
| 673 | Will be set by the formatter method. |
| 674 | """ |
| 675 | return self._format |
| 676 | |
| 677 | def __str__(self): |
| 678 | return self._snippet |
| 679 | |
| 680 | def __repr__(self): |
| 681 | return f'Snippet("{self.snippet}", format="{self.format}")' |
| 682 | |
| 683 | def save(self, filenameOrPath: str | Path): |
| 684 | """ |
| 685 | Saves the snippet. |
| 686 | |
| 687 | If the path is absolute, it saves it in that location. |
| 688 | Otherwise, the preffix and suffix are added according to format: |
| 689 | |
| 690 | - latex |
| 691 | |
| 692 | - prefix: project folder -> SLiCAP.ini -> [projectpaths] -> tex_snippets |
| 693 | - suffix: '.tex' |
| 694 | |
| 695 | - rst |
| 696 | |
| 697 | - prefix: project folder -> SLiCAP.ini -> [projectpaths] -> rst_snippets |
| 698 | - suffix: '.rst' |
| 699 | - expressions and inline equations are appended to the file as: |
| 700 | |
| 701 | \\|<variable name>\\| = <expr>\\|<inline equation> |
| 702 | |
| 703 | - myst |
no outgoing calls
no test coverage detected