def autoUpgrade(source, **params):
    """Turn older CNXML (0.5/0.6) into newer (0.7).
    Checks version to determine if upgrade is needed.
    With a newer version, we may add additional stylesheets to the pipeline.
    Return tuple of (new_source, boolean_was_converted)
    """
    version = Recognizer(source).getVersion(
    )  # if wrong, we could end up doing this on every save
    stylesheets = []
    if version in ('0.7', '0.8'):
        pass  # Do nothing. 0.7 is the latest
    elif version == '0.6':
        stylesheets.append(UPGRADE_06_TO_07_XSL)
    else:
        stylesheets.append(UPGRADE_05_TO_06_XSL)
        stylesheets.append(UPGRADE_06_TO_07_XSL)

    if source and stylesheets:
        try:
            doc = XMLService.parseString(source)
            result = XMLService.xsltPipeline(doc, stylesheets, **params)
            return result, True
        except XMLService.XMLParserError:
            pass  # just stopping on parse error is okay; it'll send us to the fallback below
    return source, False
def autoIds(source, prefix=None, force=False, **params):
    """For CNXML, fill in ids where the author has left none. Only for CNXML 0.6, and will check
    for CNXML version unless instructed otherwise.
    'prefix' will add the passed-in string to the beginning of the ids
    'force' doesn't check version itself, relies on caller to certify the XML is okay.
    """
    transformable = force or Recognizer(source).getVersion() in ('0.6', '0.7')
    # we want to make sure recognizer keeps working right; if wrong, we end up doing this on every save

    if prefix:
        params['id-prefix'] = prefix
    stylesheets = [CNXML_AUTOID_XSL]
    if source and stylesheets and transformable:
        try:
            doc = XMLService.parseString(source)   # may error if malformed
            # TODO: if slow, we could replace with Expat 2-pass method
            result = XMLService.xsltPipeline(doc, stylesheets, **params)
            return result
        except XMLService.XMLParserError:
            pass  # just stopping on parse error is okay; it'll send us to the fallback below
    return source
def autoIds(source, prefix=None, force=False, **params):
    """For CNXML, fill in ids where the author has left none. Only for CNXML 0.6, and will check
    for CNXML version unless instructed otherwise.
    'prefix' will add the passed-in string to the beginning of the ids
    'force' doesn't check version itself, relies on caller to certify the XML is okay.
    """
    transformable = force or Recognizer(source).getVersion() in ('0.6', '0.7')
    # we want to make sure recognizer keeps working right; if wrong, we end up doing this on every save

    if prefix:
        params['id-prefix'] = prefix
    stylesheets = [CNXML_AUTOID_XSL]
    if source and stylesheets and transformable:
        try:
            doc = XMLService.parseString(source)  # may error if malformed
            # TODO: if slow, we could replace with Expat 2-pass method
            result = XMLService.xsltPipeline(doc, stylesheets, **params)
            return result
        except XMLService.XMLParserError:
            pass  # just stopping on parse error is okay; it'll send us to the fallback below
    return source
def autoUpgrade(source, **params):
    """Turn older CNXML (0.5/0.6) into newer (0.7).
    Checks version to determine if upgrade is needed.
    With a newer version, we may add additional stylesheets to the pipeline.
    Return tuple of (new_source, boolean_was_converted)
    """
    version = Recognizer(source).getVersion() # if wrong, we could end up doing this on every save
    stylesheets = []
    if version == '0.7':
        pass # Do nothing. 0.7 is the latest
    elif version == '0.6':
        stylesheets.append(UPGRADE_06_TO_07_XSL)
    else:
        stylesheets.append(UPGRADE_05_TO_06_XSL)
        stylesheets.append(UPGRADE_06_TO_07_XSL)

    if source and stylesheets:
        try:
            doc = XMLService.parseString(source)
            result = XMLService.xsltPipeline(doc, stylesheets, **params)
            return result, True
        except XMLService.XMLParserError:
            pass  # just stopping on parse error is okay; it'll send us to the fallback below
    return source, False