diff --git a/mwparserfromhell/parser/contexts.py b/mwparserfromhell/parser/contexts.py index af6dea6..cac5250 100644 --- a/mwparserfromhell/parser/contexts.py +++ b/mwparserfromhell/parser/contexts.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012-2017 Ben Kurtovic +# Copyright (C) 2012-2019 Ben Kurtovic # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -193,3 +193,16 @@ UNSAFE = (TEMPLATE_NAME + WIKILINK_TITLE + EXT_LINK_TITLE + DOUBLE = TEMPLATE_PARAM_KEY + TAG_CLOSE + TABLE_ROW_OPEN NO_WIKILINKS = TEMPLATE_NAME + ARGUMENT_NAME + WIKILINK_TITLE + EXT_LINK_URI NO_EXT_LINKS = TEMPLATE_NAME + ARGUMENT_NAME + WIKILINK_TITLE + EXT_LINK + +def describe(context): + """Return a string describing the given context value, for debugging.""" + flags = [] + for name, value in globals().items(): + if not isinstance(value, int) or name.startswith("GL_"): + continue + if bin(value).count("1") != 1: + continue # Hacky way to skip aggregate contexts + if context & value: + flags.append((name, value)) + flags.sort(key=lambda it: it[1]) + return "|".join(it[0] for it in flags) diff --git a/tests/test_pytokenizer.py b/tests/test_pytokenizer.py index 85a55b9..a4c9bc1 100644 --- a/tests/test_pytokenizer.py +++ b/tests/test_pytokenizer.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2012-2016 Ben Kurtovic +# Copyright (C) 2012-2019 Ben Kurtovic # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -23,6 +23,7 @@ from __future__ import unicode_literals import unittest +from mwparserfromhell.parser import contexts from mwparserfromhell.parser.tokenizer import Tokenizer from ._test_tokenizer import TokenizerTestCase @@ -40,5 +41,10 @@ class TestPyTokenizer(TokenizerTestCase, unittest.TestCase): self.assertFalse(Tokenizer.USES_C) self.assertFalse(Tokenizer().USES_C) + def test_describe_context(self): + self.assertEqual("", contexts.describe(0)) + ctx = contexts.describe(contexts.TEMPLATE_PARAM_KEY|contexts.HAS_TEXT) + self.assertEqual("TEMPLATE_PARAM_KEY|HAS_TEXT", ctx) + if __name__ == "__main__": unittest.main(verbosity=2)