pipes are common sugar¤
the |
is a common operator in programming languages.
in pidgy, we find the |
operator in jijnja2
filters.
here we adds pipes to markdown strings used in pidgy
.
the pipe
classes¤
make it possible chain markdown and python.
class pipe(functools.partial):
def __or__(self, object):
if isinstance(object, str): return super().__call__(object)
if callable(object): return pipe(lambda x: object(super().__call__(x)))
raise NotImplementedError(F"not implemented for {type(object)}")
def __ror__(self, object):
if isinstance(object, str): return super().__call__(object)
if callable(object): return pipe(lambda x: super().__call__(object(x)))
class do(pipe):
def __call__(self, *args, **kwargs):
super().__call__(*args, **kwargs)
object, *_ = args + (None,)
return object
pipe
can used as a decorator
@pipe
def strip_fence(body):
lines = body.splitlines(1)[1:]
if lines[-1].startswith(("```", "~~~")):
lines.pop()
return "".join(lines)
maybe we want a pipeable version of print
print = do(print)
print | "asdf"
x=(
```test
crap
```
)| strip_fence | do(print)