Skip to content

Instantly share code, notes, and snippets.

@buchi
Created July 15, 2013 12:22
Show Gist options
  • Save buchi/5999560 to your computer and use it in GitHub Desktop.
Save buchi/5999560 to your computer and use it in GitHub Desktop.
# deco-grid maker
# calculates widths and positions for the deco grid system for arbitrary
# number of columns, cell width and gutter (margin).
# author: Thomas Buchberger <[email protected]>
# last modified: Di 2 Nov 2010 18:05:36 CET
from optparse import OptionParser
def main():
parser = OptionParser()
parser.add_option("-c", "--columns", dest="columns", type="int", default=16)
parser.add_option("-w", "--cell-width", dest="cell_width", type="int", default=40)
parser.add_option("-g", "--gutter", dest="gutter", type="int", default=20)
parser.add_option("-m", "--margin", dest="margin", type="int", default=10)
parser.add_option("-r", "--relative", action="store_true", dest="relative", default=False)
parser.add_option("-p", "--precision", dest="precision", type="int", default=5)
(options, args) = parser.parse_args()
cell_gutter = options.cell_width + options.gutter
#wtotal = options.columns*(options.cell_width+options.gutter)
wtotal = options.columns*options.cell_width+(options.columns-1)*options.gutter+2*options.margin
print "total width: %spx." % wtotal
for w in range(1, options.columns+1):
ww = (cell_gutter)*w-options.gutter
if options.relative:
print "div.width-%s { width:%s%%; }" % (w, round(100*float(ww)/float(wtotal),options.precision))
else:
print "div.width-%s { width:%spx; }" % (w, ww)
for w in range(0, options.columns):
pp = cell_gutter*w
if options.relative:
print "div.position-%s { margin-left:%s%%; }" % (w, round(100*float(pp)/float(wtotal)+100*float(options.margin)/float(wtotal)-100,options.precision))
else:
print "div.position-%s { margin-left:%spx; }" % (w, pp-wtotal+options.margin)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment