Skip to content

Commit

Permalink
added separate categories for types of stylesheet and am sending them…
Browse files Browse the repository at this point in the history
… out separately to the edit page
  • Loading branch information
alex-hofsteede committed Sep 26, 2011
1 parent da8a57c commit bc4495f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
6 changes: 3 additions & 3 deletions settings.py
Expand Up @@ -121,11 +121,11 @@
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'swap'
'swap',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'django.contrib.admindocs',
)

# A sample logging configuration. The only tangible logging
Expand Down
29 changes: 15 additions & 14 deletions swap/util.py
Expand Up @@ -73,16 +73,17 @@ def processPage(page_url):
page.url = page_url
page.original = page_content
page.save()
css_assets = []
css_stylesheets = []
css_tags = []

mark("save page")
page_content = makeLinksAbsolute(page_content,[u'href',u'src'], page_url)
mark("make links absolute")
page_content = parseStyleAttributes(page_content, css_assets, page)
page_content = parseStyleAttributes(page_content, css_stylesheets, page)
mark("parse style attributes")
page_content = parseStyleTags(page_content, css_assets, page)
page_content = parseStyleTags(page_content, css_stylesheets, page)
mark("parse style tags")
page_content = parseLinkedStylesheets(page_content, css_assets, page)
page_content = parseLinkedStylesheets(page_content, css_stylesheets, page)
mark("parse linked stylesheets")
clear()

Expand All @@ -91,7 +92,7 @@ def processPage(page_url):
page.save()
return page

def parseStyleAttributes(document, css_assets, page):
def parseStyleAttributes(document, css_stylesheets, page):
"""
Grabs any style="" attributes on normal html tags and saves the CSS therein.
Replaces the style with a delimited UUID tag that we can use later to re-insert the style
Expand All @@ -106,11 +107,11 @@ def parseStyleAttributes(document, css_assets, page):
css_content = value.strip("\"' ")
css_name = 'style=""'#TODO get the ID attribute from the same tag (could be difficult)
css_asset = createCSSAsset(css_content, page, css_types['ATTRIBUTE'], name=css_name)
css_assets.append(css_asset)#TODO css_assets is not necessary anymore but perhape we can save cycles by passing it diretly to editpage instead of having to get that all from the DB again
css_stylesheets.append(css_asset)#TODO css_stylesheets is not necessary anymore but perhape we can save cycles by passing it diretly to editpage instead of having to get that all from the DB again
output_tokens.append('"' + delimiter + css_asset.uuid + delimiter + '"')
return "".join(output_tokens)

def parseStyleTags(document, css_assets, page):
def parseStyleTags(document, css_stylesheets, page):
"""
Grabs any <style> tags and saves the CSS therein. replaces with a
uuid that we can use later to re-insert the style.
Expand All @@ -134,15 +135,15 @@ def parseStyleTags(document, css_assets, page):
css_content = "".join(stylesheet_tokens)
css_content = makeCSSURLsAbsolute(css_content,page.url)
css_asset = createCSSAsset(css_content, page, css_types['INLINE'], name='<style/>')
css_assets.append(css_asset)
parseNestedStylesheets(css_asset, css_assets, page)
css_stylesheets.append(css_asset)
parseNestedStylesheets(css_asset, css_stylesheets, page)
output_tokens.append( delimiter + css_asset.uuid + delimiter )
output_tokens.append(value)
elif instyle:
stylesheet_tokens.append(value)
return "".join(output_tokens)

def parseLinkedStylesheets(document, css_assets, page):
def parseLinkedStylesheets(document, css_stylesheets, page):
"""
Grabs any <link> tags that point to stylesheets, downloads and saves the
linked stylesheet, and replaces the link to our own saved version
Expand All @@ -165,14 +166,14 @@ def parseLinkedStylesheets(document, css_assets, page):
css_content = unicode(f.read(),'utf-8')
css_content = makeCSSURLsAbsolute(css_content, css_url)
css_asset = createCSSAsset(css_content, page, css_types['STYLESHEET'], css_url, css_name)
css_assets.append(css_asset)
css_stylesheets.append(css_asset)
attr_dict['href'] = u'/css/%s' % css_asset.uuid #No need to save a delimited value to regex out later. the link to /css/{uuid} will be constant
parseNestedStylesheets(css_asset, css_assets, page)
parseNestedStylesheets(css_asset, css_stylesheets, page)
output_tokens.append(" " + serializeTagAttributes(attr_dict) + " ")
output_tokens.append(close_tag)
return "".join(output_tokens)

def parseNestedStylesheets(css_asset, css_assets, page):
def parseNestedStylesheets(css_asset, css_stylesheets, page):
"""
Looks through a CSS stylesheet for any @import tags and downloads the imported stylesheets,
replacing their reference in the parent stylesheet with the link to our own saves version
Expand All @@ -192,7 +193,7 @@ def replace(match):
css_content = unicode(f.read())
css_content = makeCSSURLsAbsolute(css_content,css_url)
css_sub_asset = createCSSAsset(css_content, page, css_types['STYLESHEET'], css_url, css_name)
css_assets.append(css_sub_asset)
css_stylesheets.append(css_sub_asset)
return match.group(1) + u'/css/%s' % css_sub_asset.uuid + match.group(3)
css_asset.raw = regex.sub(replace,css_asset.raw)
css_asset.save()
Expand Down
5 changes: 3 additions & 2 deletions swap/views.py
Expand Up @@ -29,8 +29,9 @@ def getCSS(request,uuid):
#TODO protect with token in cookie (that gets generated in getpage). Don't want to allow anyone to edit pages, only the person that originally got it
def editpage(request,page_id):
page = Page.objects.get(pk=page_id) #TODO use uuids
stylesheets = CSSAsset.objects.filter(page=page.id)
return render_to_response('edit.html', {'page':page,'stylesheets':stylesheets}, context_instance=RequestContext(request))
stylesheets = CSSAsset.objects.filter(page=page.id,type__in=[util.css_types["STYLESHEET"],util.css_types["INLINE"]])
styleattributes = CSSAsset.objects.filter(page=page.id,type__in=[util.css_types["ATTRIBUTE"]])
return render_to_response('edit.html', {'page':page,'stylesheets':stylesheets,'styleattributes':styleattributes}, context_instance=RequestContext(request))

#TODO move util functions like this out of views.
def getCSSByUUID(uuid):
Expand Down
6 changes: 6 additions & 0 deletions templates/edit.html
Expand Up @@ -14,6 +14,12 @@ <h2>Edit CSS</h2>
<textarea name="cssasset_{{stylesheet.uuid}}">{{stylesheet.raw}}</textarea>
</div>
{% endfor %}

<div id="tabs-attributes">
{% for styleattribute in styleattributes %}
<textarea name="cssasset_{{styleattribute.uuid}}">{{styleattribute.raw}}</textarea>
{% endfor %}
</div>
</div>
<input name="submit" value="Save" type="submit"></input>
</form>
Expand Down
5 changes: 3 additions & 2 deletions urls.py
@@ -1,10 +1,11 @@
from django.conf.urls.defaults import patterns, include, url
import swap
from swap import views
from django.contrib import admin

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
admin.autodiscover()

urlpatterns = patterns('',
# Examples:
Expand All @@ -23,5 +24,5 @@
#get CSS by UUID
url(r'^css/(?P<uuid>[0-f]+)/?', swap.views.getCSS),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', include(admin.site.urls)),
)

0 comments on commit bc4495f

Please sign in to comment.