diff --git a/CHANGES.rst b/CHANGES.rst index 19cc27c..7a23a6e 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,9 @@ History 2.3.2 (unreleased) ------------------ +- Parse value if they are callable, for HTML5 Data-Attributes + [2silver, 2019-06-12] + - Use ``logger.warning`` instead of deprecated ``logger.warn``. [rnix] diff --git a/src/yafowil/utils.py b/src/yafowil/utils.py index ba9ac6f..30b8bc6 100644 --- a/src/yafowil/utils.py +++ b/src/yafowil/utils.py @@ -279,6 +279,18 @@ def as_data_attrs(data): continue if val is UNSET: continue + + # this probably could be done much better! + # 2silver, 2019-06-11 + if callable(val): + try: + val = val() + except TypeError: + logging.warning( + 'converting data-attribute for "{}" failed'.format(key) + ) + val = str(val) + # convert value to JSON dump if no string. if not isinstance(val, STR_TYPE): # also remove leading and trailing double quotes,