Ejemplo n.º 1
0
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
    """
    from invenio.dateutils import get_i18n_month_name
    from invenio.messages 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
Ejemplo n.º 2
0
def format_element(bfo):
    """
    Formats a header used in Bulletin Articles containing: issue nr., date,
    english/french link, report number
    """
    # get variables
    this_recid = bfo.control_field('001')
    available_languages = bfo.fields('041__a')
    current_language = bfo.lang
    report_number = bfo.fields('037__a')
    issue_numbers = bfo.fields('773__n')
    journal_name = bfo.field('773__t')
    if journal_name.lower() == 'cern bulletin':
        journal_name = 'CERNBulletin'
    elif journal_name.lower() == 'cms bulletin':
        journal_name = 'CMSBulletin'

    preferred_ln = get_journal_preferred_language(journal_name, bfo.lang)

    date = ''
    if len(issue_numbers) > 0:
#        date = get_release_datetime(issue_numbers[0], journal_name)
#        if not date:
        date = issue_to_datetime(issue_numbers[0], journal_name)
        weekday = get_i18n_day_name(date.isoweekday() % 7)
        monthname = get_i18n_month_name(date.month)
        date  = weekday + ' ' + date.strftime('%d') + ' ' + monthname + ' ' + date.strftime('%Y')

    # assemble the HTML output
    out = '<div id="top"><div id="topbanner">&nbsp;</div>'
    if len(issue_numbers) > 0:
        out += '<span class="printLogo">%s, %s</span>' % (" & ".join(["%s" % issue.split("/")[0] for issue in issue_numbers]), date)
    out += '<div id="mainmenu"><table width="100%">'
    out += '<tr>'
    if len(issue_numbers) > 0:
        issue_number, issue_year = issue_numbers[-1].split('/')
        out += '<td class="left"><a href="%s/journal/CERNBulletin/%s/%s" target="_blank">%s: %s, %s</a></td>' % \
               (CFG_SITE_URL, issue_year, issue_number,
                cfg_messages["published_in"][preferred_ln],
                " & ".join(["%s" % issue for issue in issue_numbers]),
                date)
    if len(report_number) > 0:
        out += '<td class="right">%s</td>' % report_number[0]
    out += '</tr>'

    out += '<tr>'
    if len(available_languages) > 1:
        if current_language == "en" and "fr" in available_languages:
            #TODO: server name generic
            out += '<td class="left"><a href="%s/record/%s?ln=fr">&gt;&gt; french version</a></td>' % (CFG_SITE_URL, this_recid)
        elif current_language == "fr" and "en" in available_languages:
            out += '<td class="left"><a href="%s/record/%s?ln=en">&gt;&gt; version anglaise</a></td>' % (CFG_SITE_URL, this_recid)

    out += '<td class="right"></td>'
    out += '</tr>'
    out += '</table></div><div id="mainphoto"></div></div>'

    return out
            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
Ejemplo n.º 4
0
                             '%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+')
Ejemplo n.º 5
0
        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>'
Ejemplo n.º 6
0
        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 + ']')
Ejemplo n.º 7
0
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.dateutils import get_i18n_month_name
    from invenio.messages 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