Skip to content

Commit

Permalink
fix pandoc2ansi and tests on ubuntu
Browse files Browse the repository at this point in the history
  • Loading branch information
FredHappyface committed Mar 30, 2024
1 parent e4c9325 commit e671b6b
Show file tree
Hide file tree
Showing 10 changed files with 668 additions and 628 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All major and minor version changes will be documented in this file. Details of
patch-level version changes can be found in [commit messages](../../commits/master).

## 2024.1 - 2024/03/30

- fix `pandoc2ansi` and tests on ubuntu

## 2024 - 2024/01/07

- update dependencies
Expand Down
31 changes: 21 additions & 10 deletions catpandoc/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,39 @@
from __future__ import annotations

import argparse
import re
from io import StringIO

import pypandoc
from rich.console import Console
from rich.markdown import Markdown


def pandoc2ansi(file: str, width: int = 79) -> str:
console = Console(width=width)
string = StringIO()
console = Console(file=string, color_system="truecolor", safe_box=False, width=width)
pypandoc.ensure_pandoc_installed()
markdown = Markdown(pypandoc.convert_file(file, "md"))
with console.capture() as capture:
console.print(markdown)
return capture.get()
console.print(markdown)
return string.getvalue()

def stripAnsi(string: str) -> str:
"""Strip ansi codes from a given string.
Args:
----
string (str): string to strip codes from
Returns:
-------
str: plaintext, utf-8 string (safe for writing to files)
"""
return re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])").sub("", string)


def pandoc2plain(file: str, width: int = 79) -> str:
console = Console(color_system=None, width=width)
pypandoc.ensure_pandoc_installed()
markdown = Markdown(pypandoc.convert_file(file, "md"))
with console.capture() as capture:
console.print(markdown)
return capture.get()
return stripAnsi(pandoc2ansi(file, width))


def handle(args: argparse.Namespace) -> None:
Expand Down
33 changes: 29 additions & 4 deletions documentation/reference/catpandoc/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
- [handle](#handle)
- [pandoc2ansi](#pandoc2ansi)
- [pandoc2plain](#pandoc2plain)
- [stripAnsi](#stripansi)

## cli

[Show source in application.py:52](../../../catpandoc/application.py#L52)
[Show source in application.py:65](../../../catpandoc/application.py#L65)

Parse args from the command line.

Expand All @@ -26,7 +27,7 @@ def cli() -> None: ...

## handle

[Show source in application.py:30](../../../catpandoc/application.py#L30)
[Show source in application.py:43](../../../catpandoc/application.py#L43)

Handle the args and output to the terminal.

Expand All @@ -45,7 +46,7 @@ def handle(args: argparse.Namespace) -> None: ...

## pandoc2ansi

[Show source in application.py:14](../../../catpandoc/application.py#L14)
[Show source in application.py:16](../../../catpandoc/application.py#L16)

#### Signature

Expand All @@ -57,10 +58,34 @@ def pandoc2ansi(file: str, width: int = 79) -> str: ...

## pandoc2plain

[Show source in application.py:22](../../../catpandoc/application.py#L22)
[Show source in application.py:39](../../../catpandoc/application.py#L39)

#### Signature

```python
def pandoc2plain(file: str, width: int = 79) -> str: ...
```



## stripAnsi

[Show source in application.py:24](../../../catpandoc/application.py#L24)

Strip ansi codes from a given string.

#### Arguments

----
- `string` *str* - string to strip codes from

#### Returns

-------
- `str` - plaintext, utf-8 string (safe for writing to files)

#### Signature

```python
def stripAnsi(string: str) -> str: ...
```
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "catpandoc"
version = "2024"
version = "2024.1"
license = "mit"
description = "Cat multiple document files to the terminal"
authors = ["FredHappyface"]
Expand Down Expand Up @@ -30,12 +30,12 @@ pypandoc = "<2,>=1.13"
rich = "<14,>=13.7.1"

[tool.poetry.group.dev.dependencies]
tox = "^4.14.1"
tox = "^4.14.2"
pytest = "^8.1.1"
handsdown = "^2.1.0"
coverage = "^7.4.4"
ruff = "^0.3.3"
pyright = "^1.1.354"
pyright = "^1.1.356"

[build-system]
requires = ["poetry-core"]
Expand Down
452 changes: 226 additions & 226 deletions tests/data/catpandoc.ansi

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions tests/data/catpandoc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Version
<!-- omit in toc -->


┌─────────────────────────────────────────────────────────────────────────────┐
CatPandoc
└─────────────────────────────────────────────────────────────────────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
CatPandoc
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

<img src="readme-assets/icons/name.png" alt="Project Icon" width="750">{=html}

Expand Down
Loading

0 comments on commit e671b6b

Please sign in to comment.