'Iterate over html stash and restore "safe" html.'

def run(self, text):

    replacements = OrderedDict()
    for i in range(self.markdown.htmlStash.html_counter):
        (html, safe) = self.markdown.htmlStash.rawHtmlBlocks[i]
        if (self.markdown.safeMode and (not safe)):
            if (str(self.markdown.safeMode).lower() == u'escape'):
                html = self.escape(html)
            elif (str(self.markdown.safeMode).lower() == u'remove'):
                html = u''
            else:
                html = self.markdown.html_replacement_text
        if (self.isblocklevel(html) and (safe or (not self.markdown.safeMode))):
            replacements[(u'<p>%s</p>' % self.markdown.htmlStash.get_placeholder(i))] = (html + u'\n')
        replacements[self.markdown.htmlStash.get_placeholder(i)] = html
    if replacements:
        pattern = re.compile(u'|'.join((re.escape(k) for k in replacements)))
        text = pattern.sub((lambda m: replacements[m.group(0)]), text)
    return text
