Skip to content
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

Add validation for Pango Markup - Fixes #8 #9

Merged
merged 9 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions manimpango/cmanimpango.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cdef extern from "glib.h":
ctypedef void* gpointer
ctypedef int gint
ctypedef unsigned int guint
ctypedef unsigned int guint32
ctypedef gint gboolean
ctypedef unsigned short guint16
void g_object_unref(gpointer object)
Expand Down Expand Up @@ -34,6 +35,17 @@ cdef extern from "cairo-svg.h":
double height_in_points
)

cdef extern from "pango/pango-attributes.h":
gboolean pango_parse_markup(
const char *markup_text,
int length,
void* accel_marker,
void* attr_list,
void* text,
void* accel_char,
void* error
)

cdef extern from "pango/pangocairo.h":
int PANGO_SCALE
int pango_units_from_double(double d)
Expand Down
5 changes: 5 additions & 0 deletions manimpango/cmanimpango.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ def text2svg(
return file_name

class MarkupUtils:
@staticmethod
def validate(text: str) -> bool:
text_bytes = text.encode("utf-8")
return pango_parse_markup(text_bytes, len(text_bytes), NULL, NULL, NULL, NULL, NULL)

@staticmethod
def text2svg(
text: str,
Expand Down
21 changes: 21 additions & 0 deletions tests/test_markup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
def test_good_markup():
import manimpango # noqa: F401

assert manimpango.MarkupUtils.validate(
"foo"
), '"foo" should not fail validation'
assert manimpango.MarkupUtils.validate(
"<b>bar</b>"
), '"<b>bar</b>" should not fail validation'


def test_bad_markup():
import manimpango # noqa: F401

assert not manimpango.MarkupUtils.validate(
"<b>foo"
), '"<b>foo" should fail validation (unbalanced tags)'
assert not manimpango.MarkupUtils.validate(
"<xyz>foo</xyz>"
), '"<xyz>foo</xyz>" should fail validation (invalid tag)'