[Moin-user] Inject CSS for a particular page

Jim Wight jkwight at gmail.com
Tue Jul 23 04:09:10 EDT 2013


On 21 July 2013 15:29, Paul Boddie <paul at boddie.org.uk> wrote:

> On Sunday 21 July 2013 06:36:01 Emmanuel Mayssat wrote:
> > I would like to change the style of headers for a subset of pages.
> > How can I inject my css stylesheet in a particular page?
>
> For all pages, you can obviously change the "stylesheets" configuration
> setting like this (not tested):
>
>   stylesheets = [("screen", "/css/mystyles.css")]
>
> This augments the stylesheets mentioned in the theme class. See the
> ThemeBase
> class in MoinMoin/theme/__init__.py for the default definitions.
>
> To affect only some pages, it might be necessary to change the theme code
> or
> to override a theme with extra code to insert references to styles and
> scripts (with regard to your other question).
>

How about using a macro for the some pages situation? E.g.

def macro_InjectJSorCSS(macro, _trailing_args=[]):

    args = {'jquery':'http://code.jquery.com/jquery.min.js',
            'somejs':/server/path/to/somejs.js',
            'somecss':'/filesystem/path/to/somecss.css'}

    html = ''
    for arg in _trailing_args:
        src = args[arg]

        if src.endswith('js'):
            html += '<script type="text/javascript" src="%s"></script>' %
src
        else:
            html += '<style type="text/css">' + open(src, 'r').read() +
'</style>'

    return macro.formatter.rawHTML(html)

Thus, InjectJSorCSS(jquery,somejs,somecss)

The content of CSS files is injected into the page as the <link> statement
is restricted to the head.

Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/moin-user/attachments/20130723/b0b75f30/attachment.html>


More information about the Moin-user mailing list