# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import json
import demjson
import yaml
import re
import unwrapper


def unwrap_and_load(content):
    cleaned = clean_json(unwrapper.unwrap_raw(content))
    return loads(cleaned)


def loads(content):
    cleaned = clean_json(content)
    try:
        # strip out HTML content and unescaped chars
        return json.loads(cleaned)
    except Exception:
        try:
            # try loading it as YAML
            return yaml.load(cleaned)
        except Exception:
            # try using demjson to decode a non-strict json string
            return demjson.decode(cleaned)


def clean_json(content):
    return remove_html(remove_bad_double_quotes(remove_invalid_escapes(add_missing_commas(content))))


def sub_first(match):
    return match.group(1) + '"'


def sub_last(match):
    return '"' + match.group(1)


def remove_bad_double_quotes(content):
    # JSON requires values to be surrounded in " 's, ie `{"foo": "bar"}`. This handles
    # cases where the JSON is like `{"foo":""bar""}`--- json.loads() won't like this case !
    return re.sub('([^\:])\"\"', sub_first, re.sub(r'\"\"([^,])', sub_last, content))


def remove_html(content):
    return re.sub('<[^<]+?>', '', content)


def remove_invalid_escapes(content):
    return content.replace("\\", "")


def add_missing_commas(content):
    return content.replace('"}', '",}')

@@ -764,24 +764,521 @@
 ntent))))%0A%0A%0A
+def process_repl(match):%0A    return match.group(2)%0A%0A%0Adef remove_bad_blocks(block_pairs, content):%0A    x = content%0A    for pair in block_pairs:%0A        x = remove_bad_block(pair%5B0%5D, pair%5B1%5D, x)%0A%0A    return x%0A%0A%0Adef remove_bad_block(key_name_to_rem, key_to_stop_rem_at, content):%0A    if '%22%7B%7D%22'.format(key_name_to_rem) in content:%0A        x = re.sub('(%5C%22%7B%7D%5C%22:.%7B%7B1,%7D%7D)(%5C%22%7B%7D%5C%22)'.format(key_name_to_rem, key_to_stop_rem_at), process_repl, content, flags=re.DOTALL)%0A        return x%0A%0A    return content%0A%0A%0A
 def sub_firs

add some general code for removing bad-blocks based on keys before the content is parsed