def test_convert_good_cvsdate(self): """dateutils - conversion of good CVS dates""" # here we have to use '$' + 'Date...' here, otherwise the CVS # commit would erase this time format to put commit date: datecvs = "$" + "Date: 2006/09/21 10:07:22 $" datestruct_beginning_expected = (2006, 9, 21, 10, 7, 22) self.assertEqual(dateutils.convert_datecvs_to_datestruct(datecvs)[:6], datestruct_beginning_expected) datecvs = "$Id: dateutils_tests.py,v 1.6 2007/02/14 18:33:02 tibor " \ "Exp $" datestruct_beginning_expected = (2007, 2, 14, 18, 33, 2) self.assertEqual(dateutils.convert_datecvs_to_datestruct(datecvs)[:6], datestruct_beginning_expected)
def tmpl_pagefooter(self, req, **kwargs): """Creates a page footer Parameters: - 'ln' *string* - The language to display - 'lastupdated' *string* - when the page was last updated - 'pagefooteradd' *string* - additional page footer HTML code Output: - HTML code of the page headers """ ctx = dict(ln=CFG_SITE_LANG, lastupdated=None, pagefooteradd=None) ctx.update(kwargs) lastupdated = ctx.get('lastupdated') if lastupdated and lastupdated != '$Date$': if lastupdated.startswith("$Date: ") or lastupdated.startswith("$Id: "): ctx['lastupdated'] = convert_datecvs_to_datestruct(lastupdated) return render_template_to_string( "legacy_page.html", no_pagebody=True, no_pageheader=True, **ctx ).encode('utf8')
def tmpl_pagefooter(self, req, **kwargs): """Creates a page footer Parameters: - 'ln' *string* - The language to display - 'lastupdated' *string* - when the page was last updated - 'pagefooteradd' *string* - additional page footer HTML code Output: - HTML code of the page headers """ ctx = dict(ln=CFG_SITE_LANG, lastupdated=None, pagefooteradd=None) ctx.update(kwargs) lastupdated = ctx.get('lastupdated') if lastupdated and lastupdated != '$Date$': if lastupdated.startswith("$Date: ") or lastupdated.startswith( "$Id: "): ctx['lastupdated'] = convert_datecvs_to_datestruct(lastupdated) return render_template_to_string("legacy_page.html", no_pagebody=True, no_pageheader=True, **ctx).encode('utf8')
def test_convert_bad_cvsdate(self): """dateutils - conversion of bad CVS dates""" # here we have to use '$' + 'Date...' here, otherwise the CVS # commit would erase this time format to put commit date: datecvs = "$" + "Date: 2006/AA/21 10:07:22 $" datestruct_beginning_expected = (0, 0, 0, 0, 0, 0) self.assertEqual(dateutils.convert_datecvs_to_datestruct(datecvs)[:6], datestruct_beginning_expected)
def transform(webdoc_source, verbose=0, req=None, languages=CFG_SITE_LANGS): """ Transform a WebDoc into html This is made through a serie of transformations, mainly substitutions. Parameters: - webdoc_source : *string* the WebDoc input to transform to HTML """ parameters = {} # Will store values for specified parameters, such # as 'Title' for <!-- WebDoc-Page-Title: Title --> def get_param_and_remove(match): """ Analyses 'match', get the parameter and return empty string to remove it. Called by substitution in 'transform(...)', used to collection parameters such as <!-- WebDoc-Page-Title: Title --> @param match: a match object corresponding to the special tag that must be interpreted """ tag = match.group("tag") value = match.group("value") parameters[tag] = value return '' def translate(match): """ Translate matching values """ word = match.group("word") translated_word = _(word) return translated_word # 1 step ## First filter, used to remove comments ## and <protect> tags uncommented_webdoc = '' for line in webdoc_source.splitlines(True): if not line.strip().startswith('#'): uncommented_webdoc += line webdoc_source = uncommented_webdoc.replace('<protect>', '') webdoc_source = webdoc_source.replace('</protect>', '') html_texts = {} # Language dependent filters for ln in languages: _ = gettext_set_language(ln) # Check if translation is really needed ## Just a quick check. Might trigger false negative, but it is ## ok. if ln != CFG_SITE_LANG and \ translation_pattern.search(webdoc_source) is None and \ pattern_lang_link_current.search(webdoc_source) is None and \ pattern_lang_current.search(webdoc_source) is None and \ '<%s>' % ln not in webdoc_source and \ ('_(') not in webdoc_source: continue # 2 step ## Filter used to translate string in _(..)_ localized_webdoc = translation_pattern.sub(translate, webdoc_source) # 3 step ## Print current language 'en', 'fr', .. instead of ## <lang:current /> tags and '?ln=en', '?ln=fr', .. instead of ## <lang:link /> localized_webdoc = pattern_lang_link_current.sub( '?ln=' + ln, localized_webdoc) localized_webdoc = pattern_lang_current.sub(ln, localized_webdoc) # 4 step ## Filter out languages localized_webdoc = filter_languages(localized_webdoc, ln, defined_tags) # 5 Step ## Replace defined tags with their value from config file ## Eg. replace <CFG_SITE_URL> with 'http://cds.cern.ch/': for defined_tag, value in iteritems(defined_tags): if defined_tag.upper() == '<CFG_SITE_NAME_INTL>': localized_webdoc = localized_webdoc.replace(defined_tag, \ value.get(ln, value['en'])) else: localized_webdoc = localized_webdoc.replace(defined_tag, value) # 6 step ## Get the parameters defined in HTML comments, like ## <!-- WebDoc-Page-Title: My Title --> localized_body = localized_webdoc for tag, pattern in iteritems(pattern_tags): localized_body = pattern.sub(get_param_and_remove, localized_body) out = localized_body # Pre-process date last_updated = parameters.get('WebDoc-Page-Revision', '') last_updated = convert_datecvs_to_datestruct(last_updated) last_updated = convert_datestruct_to_datetext(last_updated) html_texts[ln] = (ln, out, parameters.get('WebDoc-Page-Title'), parameters.get('WebDoc-Page-Keywords'), parameters.get('WebDoc-Page-Navtrail'), last_updated, parameters.get('WebDoc-Page-Description')) # Remove duplicates filtered_html_texts = [] if CFG_SITE_LANG in html_texts: filtered_html_texts = [(html_text[0], \ (html_text[1] != html_texts[CFG_SITE_LANG][1] and html_text[1]) or None, \ (html_text[2] != html_texts[CFG_SITE_LANG][2] and html_text[2]) or None, \ (html_text[3] != html_texts[CFG_SITE_LANG][3] and html_text[3]) or None, \ (html_text[4] != html_texts[CFG_SITE_LANG][4] and html_text[4]) or None, \ (html_text[5] != html_texts[CFG_SITE_LANG][5] and html_text[5]) or None, \ (html_text[6] != html_texts[CFG_SITE_LANG][6] and html_text[6]) or None) for html_text in html_texts.values() \ if html_text[0] != CFG_SITE_LANG] filtered_html_texts.append(html_texts[CFG_SITE_LANG]) else: filtered_html_texts = html_texts.values() return filtered_html_texts
def transform(webdoc_source, verbose=0, req=None, languages=CFG_SITE_LANGS): """ Transform a WebDoc into html This is made through a serie of transformations, mainly substitutions. Parameters: - webdoc_source : *string* the WebDoc input to transform to HTML """ parameters = {} # Will store values for specified parameters, such # as 'Title' for <!-- WebDoc-Page-Title: Title --> def get_param_and_remove(match): """ Analyses 'match', get the parameter and return empty string to remove it. Called by substitution in 'transform(...)', used to collection parameters such as <!-- WebDoc-Page-Title: Title --> @param match: a match object corresponding to the special tag that must be interpreted """ tag = match.group("tag") value = match.group("value") parameters[tag] = value return "" def translate(match): """ Translate matching values """ word = match.group("word") translated_word = _(word) return translated_word # 1 step ## First filter, used to remove comments ## and <protect> tags uncommented_webdoc = "" for line in webdoc_source.splitlines(True): if not line.strip().startswith("#"): uncommented_webdoc += line webdoc_source = uncommented_webdoc.replace("<protect>", "") webdoc_source = webdoc_source.replace("</protect>", "") html_texts = {} # Language dependent filters for ln in languages: _ = gettext_set_language(ln) # Check if translation is really needed ## Just a quick check. Might trigger false negative, but it is ## ok. if ( ln != CFG_SITE_LANG and translation_pattern.search(webdoc_source) is None and pattern_lang_link_current.search(webdoc_source) is None and pattern_lang_current.search(webdoc_source) is None and "<%s>" % ln not in webdoc_source and ("_(") not in webdoc_source ): continue # 2 step ## Filter used to translate string in _(..)_ localized_webdoc = translation_pattern.sub(translate, webdoc_source) # 3 step ## Print current language 'en', 'fr', .. instead of ## <lang:current /> tags and '?ln=en', '?ln=fr', .. instead of ## <lang:link /> localized_webdoc = pattern_lang_link_current.sub("?ln=" + ln, localized_webdoc) localized_webdoc = pattern_lang_current.sub(ln, localized_webdoc) # 4 step ## Filter out languages localized_webdoc = filter_languages(localized_webdoc, ln, defined_tags) # 5 Step ## Replace defined tags with their value from config file ## Eg. replace <CFG_SITE_URL> with 'http://cds.cern.ch/': for defined_tag, value in iteritems(defined_tags): if defined_tag.upper() == "<CFG_SITE_NAME_INTL>": localized_webdoc = localized_webdoc.replace(defined_tag, value.get(ln, value["en"])) else: localized_webdoc = localized_webdoc.replace(defined_tag, value) # 6 step ## Get the parameters defined in HTML comments, like ## <!-- WebDoc-Page-Title: My Title --> localized_body = localized_webdoc for tag, pattern in iteritems(pattern_tags): localized_body = pattern.sub(get_param_and_remove, localized_body) out = localized_body # Pre-process date last_updated = parameters.get("WebDoc-Page-Revision", "") last_updated = convert_datecvs_to_datestruct(last_updated) last_updated = convert_datestruct_to_datetext(last_updated) html_texts[ln] = ( ln, out, parameters.get("WebDoc-Page-Title"), parameters.get("WebDoc-Page-Keywords"), parameters.get("WebDoc-Page-Navtrail"), last_updated, parameters.get("WebDoc-Page-Description"), ) # Remove duplicates filtered_html_texts = [] if CFG_SITE_LANG in html_texts: filtered_html_texts = [ ( html_text[0], (html_text[1] != html_texts[CFG_SITE_LANG][1] and html_text[1]) or None, (html_text[2] != html_texts[CFG_SITE_LANG][2] and html_text[2]) or None, (html_text[3] != html_texts[CFG_SITE_LANG][3] and html_text[3]) or None, (html_text[4] != html_texts[CFG_SITE_LANG][4] and html_text[4]) or None, (html_text[5] != html_texts[CFG_SITE_LANG][5] and html_text[5]) or None, (html_text[6] != html_texts[CFG_SITE_LANG][6] and html_text[6]) or None, ) for html_text in html_texts.values() if html_text[0] != CFG_SITE_LANG ] filtered_html_texts.append(html_texts[CFG_SITE_LANG]) else: filtered_html_texts = html_texts.values() return filtered_html_texts