예제 #1
0
def to_imgserve_url(url, maxw, maxh):
    '''
    Calculate the value of an imgserve URL for an image

    Both the maximum width and height are specified for scaling.  This
    is mainly to prevent off-by-one rounding errors: sometimes the
    code of this module will come up with a barely different scaled
    value of the height for a different resize width than Imagemagick
    will (which is currently used on the backend), which would
    mismatch the height attribute of the img tag in the final HTML
    document.

    So we just sidestep this by calculating the height in exactly one
    place - the new_img_sizes function in this library - and directing
    imgserve to scale both dimensions as we specify. This might lead
    to a slight distortion in aspect ratio, but is going to be subtle
    at worst, and worth the tradeoff.

    Example:
    to_imgserve_url('http://example.com/foo.png', 42, 70)
      -> '/_mwuimg/?src=http%3A%2F%2Fexample.com%2Ffoo.png&maxw=42&maxh=70'

    @param url  : URL pointing to the source image
    @type  url  : str

    @param maxw : Maximum desired width of the image
    @type  maxw : int

    @param maxh : Maximum desired height of the image
    @type  maxh : None, or int

    @return     : URL to the imgserve version of the URL
    @rtype      : str
    
    '''
    logger.debug('Converting img URL: {}'.format(url))
    from urllib.parse import quote
    assert maxw > 0, maxw
    imgserve_url = '/_mwuimg/?src={src}&maxw={maxw}'.format(src=quote(url, safe=''), maxw=str(maxw))
    if maxh is not None:
        imgserve_url += '&maxh=' + str(maxh)
    return imgserve_url
예제 #2
0
    def __init__(self, components, params=None, template=None, name=None, imgsubs=None, template_loader=None, **kw):
        """
        ctor

        components is an ordered list of mobilize page components (see
        L{mobilize.components}), identifying content elements in the
        mobile page body, some of which are based on content from the
        desktop page.  From this, a list of HTML snippets will be made
        available to the template as the "elements" attribute of the
        parameter dictionary.

        The params field is for a starting set of template parameters
        for final rendering.  It is optional, defaulting to an empty
        dict, and there are ways to add to this set later on.  It MUST
        NOT contain a key named "elements", as that is reserved for
        later use; the ctor will check double check this for you, so
        if you accidentally put it in, you'd get an immediate runtime
        error.

        template can be either a string, or an instance of
        jinja2.Template.  If the former, it's assumed to be the name
        of a template to load from TEMPLATE_DIRS.
        
        The imgsubs parameter can be used to specify image URL
        substitutions specific to this moplate.  If provided, a
        filters.imgsubs filter will be applied to all moplate components
        with the indicated URL substitutions.

        Because of the magical "elements" parameter, the supplied params
        cannot have a key of that name.

        @param components      : Components of content elements to extract from full body
        @type  components      : list
        
        @param params          : Starting set of rendering parameters
        @type  params          : dict (str -> mixed); no "elements" key allowed

        @param template        : Template to use
        @type  template        : str, or instance of jinja2.Template

        @param name            : Human-readable name or label for this moplate
        @type  name            : str
        
        @param imgsubs         : Image URL substitutions
        @type  imgsubs         : dict: str -> str

        @param template_loader : Alternative template loader to use
        @type  template_loader : mobilize.templates.TemplateLoader
        
        """
        from jinja2 import Template

        if template_loader is None:
            template_loader = self.default_template_loader()
        if template is None:
            template = self.DEFAULT_TEMPLATE_NAMES
        if not isinstance(template, Template):
            template = template_loader.get_template(template)
        assert isinstance(template, Template), type(template)
        logger.debug('Moplate {} using template named "{}"'.format(name, template.name))
        super().__init__(**kw)
        self.template = template
        self.components = components
        if params:
            self.params = dict(params)
        else:
            self.params = {}
        assert (
            "elements" not in self.params
        ), '"elements" is reserved/magical in mobile template params.  See Moplate class documention'
        self.imgsubs = imgsubs