Beispiel #1
0
def pre_nav_data(nav_data):
    if not isinstance(nav_data, (list, tuple)):
        return []
    new_nav_data = []
    for nav_item in nav_data:
        if isinstance(nav_item, dict):
            new_nav_data.append(nav_item)
        elif isinstance(nav_item, (list, tuple)) and len(nav_item) >=2:
            e1, e2 = nav_item[:2]
            if '/' in e1:
                new_nav_data.append(dict(url=auto_bucket_url_path(e1), name=e2))
            else:
                new_nav_data.append(dict(url=auto_bucket_url_path(e2), name=e1))
    return new_nav_data
Beispiel #2
0
    def a(cls, title, url=None, dom_id=None, must_equal=False, **kwargs):
        # 自动生成 a 元素, 主要是能产生是否 select 等属性
        # 如果不是全equal的,则只要这个起头就可以了
        classes = smart_unicode(kwargs.get('class', '') or '') or ''
        more_classes = ' ' + smart_unicode(kwargs.get('class_name', '') or '')
        classes += more_classes
        selected_classes = classes + ' selected active current'
        url = url or kwargs.get('href')
        url = auto_bucket_url_path(url)
        url = smart_unicode(url)
        title = smart_unicode(title)
        if dom_id:
            dom_id = smart_unicode(dom_id)
        if url.startswith('mailto:'):
            a_properties = ''
        elif '://' not in url and not url.startswith('#'):  # 自动补全成一个站点内的url
            url = '/' + url.lstrip('/')
            url = re.sub(r'/+', '/', url)
            a_properties = ''
        elif url.startswith('#'):
            a_properties = ''
        else:
            a_properties = 'target=_blank'
        target = kwargs.get('target')
        if target and 'target' not in a_properties and isinstance(
                target, string_types) and '"' not in target:
            a_properties += ' target="%s"' % target
        url = smart_unicode(url)
        # 不要设定 title,可能 a 内嵌的是一个 html 片段
        style = kwargs.get('style', '')
        html_for_selected = '<a href="%s" class="%s" %s>%s</a>' % (
            url, selected_classes, a_properties, title)
        html_for_normal = '<a href="%s" class="%s"  %s>%s</a>' % (
            url, classes, a_properties, title)
        if style:
            html_for_selected = html_for_selected.replace(
                '>', ' style="%s" >' % style, 1)
            html_for_normal = html_for_normal.replace('>',
                                                      ' style="%s" >' % style,
                                                      1)

        request_url = (request.path + '?' + smart_unicode(request.query_string)
                       ) if request.query_string else request.path
        request_url = re.sub(r'/page/\d+/?$', '/', request_url)
        url_to_check = url.split('#')[0]
        if must_equal:
            html_content = html_for_selected if url_to_check == request_url else html_for_normal
        else:
            if get_request_path_for_bucket(url) != '/':
                html_content = html_for_selected if request_url.startswith(
                    url_to_check) else html_for_normal
            else:
                html_content = html_for_selected if request_url == url_to_check else html_for_normal
        if dom_id:
            html_content = '<a id="%s"' % dom_id + html_content[2:]
        return html_content
Beispiel #3
0
def get_doc_url(doc):
    if not isinstance(doc, dict):
        return ''
    if not doc:
        return ''
    url = ''
    # 主要处理日志的 url 这个属性
    if 'url_path' in doc and doc.get('_type') == 'post':
        hide_post_prefix = get_site_config('hide_post_prefix',
                                           default_value=False)
        if hide_post_prefix:
            url = '/' + doc['url_path']
        else:
            url = '/post/' + doc['url_path']
    elif doc.get('_type') in ['file', 'image'] and doc.get('path'):
        url = '/' + doc['path']
    if not url:  # last
        url = doc.get('url') or ''
    if url:
        url = auto_bucket_url_path(url)
    return url
Beispiel #4
0
 def get_tag_url(self, tag):
     if isinstance(tag, (list, tuple)):
         tag = '+'.join(tag)
     url = '/tag/%s' % smart_unicode(tag)
     return auto_bucket_url_path(url)
Beispiel #5
0
 def url(self):
     v = u'/category/%s' % self.path
     return auto_bucket_url_path(v)
Beispiel #6
0
    def load(cls, *resource, **kwargs):
        if getattr(request, 'disable_load_func', False):  # load函数被禁用了
            return ''

        force_load = kwargs.pop('force', False)

        if not resource:
            return ''

        if len(resource) == 1:
            resource = resource[0]
            if resource in lazy_load_map:  # 快捷方式
                resource = lazy_load_map[resource]
            if ' ' in resource:  # load('a b c')的处理
                resource = resource.split(' ')

        # resource 可以是一个 list,也可以是字符串
        # 预先进行类型判断
        if isinstance(resource, (list, tuple)):
            result = ''
            for child_resource in resource:
                if isinstance(child_resource, string_types):
                    result += cls.load(child_resource)
            return result
        elif not isinstance(resource, string_types):
            return ''

        # 确保进入以下流程的都是单个 resource,这样才能达到去重的效果

        # 处理 smart scss 的问题
        if kwargs.pop("scss", False):
            scss_compiled_url = get_smart_scss_url(resource, **kwargs)
            resource = scss_compiled_url

        #相同的 resource,一个页面内,仅允许载入一次
        if is_resource_in_loads_in_page_already(resource) and not force_load:
            # ignore, 页面内已经载入过一次了
            return ""

        if not isinstance(resource, string_types):
            return resource

        # like h.load('jquery')
        if '.' not in resource and resource in web_static_resources_map:
            match_local_filepath = web_static_resources_map[resource]
            #match_filename = os.path.split(match_local_filepath)[-1].lower()
            relative_path = get_relative_path(match_local_filepath,
                                              root=static_folder_path)
            resource = '/__%s' % relative_path

        # url 的相对位置关系的调整~
        raw_resource = resource
        resource = auto_bucket_url_path(resource)

        # 增加 ?version 的逻辑
        if '?' not in resource:
            if resource.startswith('/__'):
                resource = '%s?version=%s' % (resource, STATIC_FILE_VERSION)
            elif '/fb_static/' in resource:
                resource = '%s?version=%s' % (resource, STATIC_FILE_VERSION)
            elif raw_resource.startswith('/template/'):
                template_pages_configs = get_pages_configs()
                template_version = template_pages_configs.get('mtime')
                if template_version:
                    resource = '%s?version=%s' % (resource, template_version)

        resource_path = resource
        if '?' in resource:
            resource_path = resource.split('?')[0]
        ext = os.path.splitext(resource_path)[1].lower()
        if not ext:
            ext = '.%s' % (resource.split('?')[0].split('/')[-1]
                           )  # 比如http://fonts.useso.com/css?family=Lato:300

        if static_files_url and resource.startswith("/fb_static/"):
            static_relative_path = resource.replace("/fb_static/", "", 1)
            static_url = "%s/%s" % (static_files_url, static_relative_path)
            resource = static_url

        if ext in [
                '.js', '.coffee'
        ] or ext.startswith('.js?') or resource.split('?')[0].endswith('js'):
            content = '<script type="text/javascript" src="%s"></script>' % resource
        elif ext in [
                '.css', '.less', '.scss', '.sass'
        ] or ext.startswith('.css?') or ext.endswith('?format=css'):
            content = '<link href="%s" type="text/css" rel="stylesheet"/>' % resource
        else:
            content = ''

        return content
Beispiel #7
0
 def url(self, url_path):
     return auto_bucket_url_path(url_path)