return create_tag('meta', name=name, content=content) # Build list of expected patterns CFG_POSSIBLE_DATE_FORMATS = [ '%Y %m %d', '%d %m %Y', '%d %B %Y', '%d %b %Y', '%Y %B %d', '%Y %b %d', '%m %Y', '%B %Y', '%b %Y', '%x', '%x %X' ] CFG_POSSIBLE_DATE_FORMATS = CFG_POSSIBLE_DATE_FORMATS + \ [f + ' %H:%M:%S' for f in CFG_POSSIBLE_DATE_FORMATS] + \ [f + 'T%H:%M:%S' for f in CFG_POSSIBLE_DATE_FORMATS] # Build month translation mapping from CFG_SITE_LANG to English. CFG_MONTH_NAMES_MAPPING = {} if CFG_SITE_LANG != 'en': CFG_MONTH_NAMES_MAPPING = dict([(get_i18n_month_name(month_nb, display='short', ln=CFG_SITE_LANG).upper(), get_i18n_month_name(month_nb, display='short', ln='en')) \ for month_nb in range(1, 13)]) CFG_MONTH_NAMES_MAPPING.update(dict([(get_i18n_month_name(month_nb, display='long', ln=CFG_SITE_LANG).upper(), get_i18n_month_name(month_nb, display='long', ln='en')) \ for month_nb in range(1, 13)])) # Build regular expression pattern to match month name in # CFG_SITE_LANG. Note the use \W instead of \b for word boundaries # because of a bug in 're' module with unicode. CFG_MONTHS_I18N_PATTERN_RE = re.compile( r"(\W|\A)(%s)(\W|\Z)" % "|".join(CFG_MONTH_NAMES_MAPPING.keys()), re.IGNORECASE | re.UNICODE) CFG_YEAR_PATTERN_RE = re.compile("((\D|\A)(\d{4})(\D|\Z))") CFG_PUNCTUATION_PATTERN_RE = re.compile(r'[' + string.punctuation + ']')
def get_month(date, ln=CFG_SITE_LANG, default=""): """ Returns the year from a textual date retrieved from a record The returned value is the 3 letters short month name in language 'ln' If year cannot be found, returns 'default' @param date: the textual date to retrieve the year from @param default: a default value to return if year not fount """ import re from invenio.utils.date import get_i18n_month_name from invenio.base.i18n import language_list_long #Look for textual month like "Jan" or "sep" or "November" or "novem" #Limit to CFG_SITE_LANG as language first (most probable date) #Look for short months. Also matches for long months short_months = [get_i18n_month_name(month).lower() for month in range(1, 13)] # ["jan","feb","mar",...] short_months_pattern = re.compile(r'('+r'|'.join(short_months)+r')', re.IGNORECASE) # (jan|feb|mar|...) result = short_months_pattern.search(date) if result is not None: try: month_nb = short_months.index(result.group().lower()) + 1 return get_i18n_month_name(month_nb, "short", ln) except: pass #Look for month specified as number in the form 2004/03/08 or 17 02 2004 #(always take second group of 2 or 1 digits separated by spaces or - etc.) month_pattern = re.compile(r'\d([\s]|[-/.,])+(?P<month>(\d){1,2})([\s]|[-/.,])') result = month_pattern.search(date) if result is not None: try: month_nb = int(result.group("month")) return get_i18n_month_name(month_nb, "short", ln) except: pass #Look for textual month like "Jan" or "sep" or "November" or "novem" #Look for the month in each language #Retrieve ['en', 'fr', 'de', ...] language_list_short = [x[0] for x in language_list_long()] for lang in language_list_short: #For each language #Look for short months. Also matches for long months short_months = [get_i18n_month_name(month, "short", lang).lower() for month in range(1, 13)] # ["jan","feb","mar",...] short_months_pattern = re.compile(r'('+r'|'.join(short_months)+r')', re.IGNORECASE) # (jan|feb|mar|...) result = short_months_pattern.search(date) if result is not None: try: month_nb = short_months.index(result.group().lower()) + 1 return get_i18n_month_name(month_nb, "short", ln) except: pass return default
def format_element(bfo, display_date='yes', display_issue_number='yes', estimate_release_date='No', granularity='', group_issues_date='yes', display_month='long', display_week_day='long'): """ Returns the string used for the issue number in the format:<br/> Issue No.<is1>-<is2>/<year> - <date>, <br/> e.g. Issue No.32-33/2007 – Tuesday 6 August 2007 if <code>estimate_release_date</code> is set to <code>yes</code>, a 'theoretical' release date is shown instead of the release date: if issue if released on Friday, display next week date. Also if journal has not been released, display an approximative release date (based on history and config) @param display_date: if 'yes', display issue date @param display_issue_number: if 'yes', display issue date @param estimate_release_date: if 'yes', display the theoretical release date @param granularity: <code>day</code>, <code>week</code> or <code>month</code> @param group_issues_date: if 'yes' and issue are grouped, display first issue date of the group @param display_month: type of display for month: 'short' ('Jan', 'Feb', etc.) or 'long' ('January', 'February', etc.) @param display_week_day: Can display day of the week ('Monday', etc.). Parameter can be 'short' ('Mon', 'Tue' etc), 'long' ('Monday', 'Tuesday', etc.) or '' (no value displayed) """ args = parse_url_string(bfo.user_info['uri']) journal_name = args["journal_name"] issue_number = args["issue"] ln = bfo.lang _ = gettext_set_language(ln) try: issue_display = get_issue_number_display(issue_number, journal_name, ln) except InvenioWebJournalJournalIdNotFoundDBError as e: return e.user_box() except Exception as e: issue_display = issue_number issues = issue_display.split("/")[0] year = issue_display.split("/")[1] week_numbers = issues.split("-") if group_issues_date.lower() == 'yes': # Get release time of this issue (do not consider issue # "updates": take the earliest issue number of this group of # issues) grouped_issues = get_grouped_issues(journal_name, issue_number) if grouped_issues: issue_number = grouped_issues[0] if estimate_release_date.lower() == 'yes': # Get theoretical release date if granularity.lower() == 'day': granularity = DAILY elif granularity.lower() == 'week': granularity = WEEKLY elif granularity.lower() == 'month': granularity = MONTHLY else: granularity = None issue_release_time = issue_to_datetime(issue_number, journal_name, granularity) else: issue_release_time = get_release_datetime(issue_number, journal_name) # Get a nice internationalized representation of this date date_text = '' if issue_release_time: if display_week_day: date_text = get_i18n_day_name(issue_release_time.isoweekday(), display_week_day, ln) + ' ' month = get_i18n_month_name(issue_release_time.month, display_month, ln=ln) date_text += issue_release_time.strftime("%d " + month + " %Y").lstrip('0') issue_url = make_journal_url(bfo.user_info['uri'], {'recid': '', 'ln': bfo.lang, 'category': ''}) out = '<a class="issue" href="%s">' % issue_url if display_issue_number.lower() == 'yes': out += _("Issue No.") + ' ' + issue_display + ' - ' if display_date.lower() == 'yes': out += date_text out += '</a>' return out
def get_month(date, ln=CFG_SITE_LANG, default=""): """ Returns the year from a textual date retrieved from a record The returned value is the 3 letters short month name in language 'ln' If year cannot be found, returns 'default' @param date: the textual date to retrieve the year from @param default: a default value to return if year not fount """ import re from invenio.utils.date import get_i18n_month_name from invenio.base.i18n import language_list_long #Look for textual month like "Jan" or "sep" or "November" or "novem" #Limit to CFG_SITE_LANG as language first (most probable date) #Look for short months. Also matches for long months short_months = [ get_i18n_month_name(month).lower() for month in range(1, 13) ] # ["jan","feb","mar",...] short_months_pattern = re.compile(r'(' + r'|'.join(short_months) + r')', re.IGNORECASE) # (jan|feb|mar|...) result = short_months_pattern.search(date) if result is not None: try: month_nb = short_months.index(result.group().lower()) + 1 return get_i18n_month_name(month_nb, "short", ln) except: pass #Look for month specified as number in the form 2004/03/08 or 17 02 2004 #(always take second group of 2 or 1 digits separated by spaces or - etc.) month_pattern = re.compile( r'\d([\s]|[-/.,])+(?P<month>(\d){1,2})([\s]|[-/.,])') result = month_pattern.search(date) if result is not None: try: month_nb = int(result.group("month")) return get_i18n_month_name(month_nb, "short", ln) except: pass #Look for textual month like "Jan" or "sep" or "November" or "novem" #Look for the month in each language #Retrieve ['en', 'fr', 'de', ...] language_list_short = [x[0] for x in language_list_long()] for lang in language_list_short: #For each language #Look for short months. Also matches for long months short_months = [ get_i18n_month_name(month, "short", lang).lower() for month in range(1, 13) ] # ["jan","feb","mar",...] short_months_pattern = re.compile(r'(' + r'|'.join(short_months) + r')', re.IGNORECASE) # (jan|feb|mar|...) result = short_months_pattern.search(date) if result is not None: try: month_nb = short_months.index(result.group().lower()) + 1 return get_i18n_month_name(month_nb, "short", ln) except: pass return default
'%d %b %Y', '%Y %B %d', '%Y %b %d', '%m %Y', '%B %Y', '%b %Y', '%x', '%x %X'] CFG_POSSIBLE_DATE_FORMATS = CFG_POSSIBLE_DATE_FORMATS + \ [f + ' %H:%M:%S' for f in CFG_POSSIBLE_DATE_FORMATS] + \ [f + 'T%H:%M:%S' for f in CFG_POSSIBLE_DATE_FORMATS] # Build month translation mapping from CFG_SITE_LANG to English. CFG_MONTH_NAMES_MAPPING = {} if CFG_SITE_LANG != 'en': CFG_MONTH_NAMES_MAPPING = dict([(get_i18n_month_name(month_nb, display='short', ln=CFG_SITE_LANG).upper(), get_i18n_month_name(month_nb, display='short', ln='en')) \ for month_nb in range(1, 13)]) CFG_MONTH_NAMES_MAPPING.update(dict([(get_i18n_month_name(month_nb, display='long', ln=CFG_SITE_LANG).upper(), get_i18n_month_name(month_nb, display='long', ln='en')) \ for month_nb in range(1, 13)])) # Build regular expression pattern to match month name in # CFG_SITE_LANG. Note the use \W instead of \b for word boundaries # because of a bug in 're' module with unicode. CFG_MONTHS_I18N_PATTERN_RE = re.compile(r"(\W|\A)(%s)(\W|\Z)" % "|".join(CFG_MONTH_NAMES_MAPPING.keys()), re.IGNORECASE | re.UNICODE) CFG_YEAR_PATTERN_RE = re.compile("((\D|\A)(\d{4})(\D|\Z))") CFG_PUNCTUATION_PATTERN_RE = re.compile(r'[' + string.punctuation + ']') CFG_SPACES_PATTERN_RE = re.compile(r'\s+')