Package development question

Question and Answer on stackoverflow

I’m wondering if/how to write a package that can find a data-attribute in markup and generate a template rendered callback based on this?