My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
#!/usr/bin/python
#
# Copyright 2008 Google Inc. All Rights Reserved.

"""Converts a LeftToRight Cascading Style Sheet into a RightToLeft one.

This is a utility script for replacing "left" oriented things in a CSS file
like float, padding, margin with "right" oriented values.
It also does the opposite.
The goal is to be able to conditionally serve one large, cat'd, compiled CSS
file appropriate for LeftToRight oriented languages and RightToLeft ones.
This utility will hopefully help your structural layout done in CSS in
terms of its RTL compatibility. It will not help with some of the more
complicated bidirectional text issues.
"""

__author__ = ['[email protected] (Lindsey Simon)']
__contributors__ = ['[email protected] (Roozbeh Pournader)',
'[email protected] (Bryon Engelhardt)']
__version__ = '0.1'

import logging
import re
import sys
import getopt
import os

import csslex

logging.getLogger().setLevel(logging.INFO)

# Global for the command line flags.
SWAP_LTR_RTL_IN_URL_DEFAULT = False
SWAP_LEFT_RIGHT_IN_URL_DEFAULT = False
IGNORE_BAD_BGP_DEFAULT = False
FLAGS = {'swap_ltr_rtl_in_url': SWAP_LTR_RTL_IN_URL_DEFAULT,
'swap_left_right_in_url': SWAP_LEFT_RIGHT_IN_URL_DEFAULT,
'ignore_bad_bgp': IGNORE_BAD_BGP_DEFAULT}

# Generic token delimiter character.
TOKEN_DELIMITER = '~'

# This is a temporary match token we use when swapping strings.
TMP_TOKEN = '%sTMP%s' % (TOKEN_DELIMITER, TOKEN_DELIMITER)

# Token to be used for joining lines.
TOKEN_LINES = '%sJ%s' % (TOKEN_DELIMITER, TOKEN_DELIMITER)

# Global constant text strings for CSS value matches.
LTR = 'ltr'
RTL = 'rtl'
LEFT = 'left'
RIGHT = 'right'

# This is a lookbehind match to ensure that we don't replace instances
# of our string token (left, rtl, etc...) if there's a letter in front of it.
# Specifically, this prevents replacements like 'background: url(bright.png)'.
LOOKBEHIND_NOT_LETTER = r'(?<![a-zA-Z])'

# This is a lookahead match to make sure we don't replace left and right
# in actual classnames, so that we don't break the HTML/CSS dependencies.
# Read literally, it says ignore cases where the word left, for instance, is
# directly followed by valid classname characters and a curly brace.
# ex: .column-left {float: left} will become .column-left {float: right}
LOOKAHEAD_NOT_OPEN_BRACE = (r'(?!(?:%s|%s|%s|#|\:|\.|\,|\+|>)*?{)' %
(csslex.NMCHAR, TOKEN_LINES, csslex.SPACE))


# These two lookaheads are to test whether or not we are within a
# background: url(HERE) situation.
# Ref: http://www.w3.org/TR/CSS21/syndata.html#uri
VALID_AFTER_URI_CHARS = r'[\'\"]?%s' % csslex.WHITESPACE
LOOKAHEAD_NOT_CLOSING_PAREN = r'(?!%s?%s\))' % (csslex.URL_CHARS,
VALID_AFTER_URI_CHARS)
LOOKAHEAD_FOR_CLOSING_PAREN = r'(?=%s?%s\))' % (csslex.URL_CHARS,
VALID_AFTER_URI_CHARS)

# Compile a regex to swap left and right values in 4 part notations.
# We need to match negatives and decimal numeric values.
# The case of border-radius is extra complex, so we handle it separately below.
# ex. 'margin: .25em -2px 3px 0' becomes 'margin: .25em 0 3px -2px'.

POSSIBLY_NEGATIVE_QUANTITY = r'((?:-?%s)|(?:inherit|auto))' % csslex.QUANTITY
POSSIBLY_NEGATIVE_QUANTITY_SPACE = r'%s%s%s' % (POSSIBLY_NEGATIVE_QUANTITY,
csslex.SPACE,
csslex.WHITESPACE)
FOUR_NOTATION_QUANTITY_RE = re.compile(r'%s%s%s%s' %
(POSSIBLY_NEGATIVE_QUANTITY_SPACE,
POSSIBLY_NEGATIVE_QUANTITY_SPACE,
POSSIBLY_NEGATIVE_QUANTITY_SPACE,
POSSIBLY_NEGATIVE_QUANTITY),
re.I)
COLOR = r'(%s|%s)' % (csslex.NAME, csslex.HASH)
COLOR_SPACE = r'%s%s' % (COLOR, csslex.SPACE)
FOUR_NOTATION_COLOR_RE = re.compile(r'(-color%s:%s)%s%s%s(%s)' %
(csslex.WHITESPACE,
csslex.WHITESPACE,
COLOR_SPACE,
COLOR_SPACE,
COLOR_SPACE,
COLOR),
re.I)

# border-radius is very different from usual 4 part notation: ABCD should
# change to BADC (while it would be ADCB in normal 4 part notation), ABC
# should change to BABC, and AB should change to BA
BORDER_RADIUS_RE = re.compile(r'((?:%s)?)border-radius(%s:%s)'
'(?:%s)?(?:%s)?(?:%s)?(?:%s)'
'(?:%s/%s(?:%s)?(?:%s)?(?:%s)?(?:%s))?' % (csslex.IDENT,
csslex.WHITESPACE,
csslex.WHITESPACE,
POSSIBLY_NEGATIVE_QUANTITY_SPACE,
POSSIBLY_NEGATIVE_QUANTITY_SPACE,
POSSIBLY_NEGATIVE_QUANTITY_SPACE,
POSSIBLY_NEGATIVE_QUANTITY,
csslex.WHITESPACE,
csslex.WHITESPACE,
POSSIBLY_NEGATIVE_QUANTITY_SPACE,
POSSIBLY_NEGATIVE_QUANTITY_SPACE,
POSSIBLY_NEGATIVE_QUANTITY_SPACE,
POSSIBLY_NEGATIVE_QUANTITY),
re.I)

# Compile the cursor resize regexes
CURSOR_EAST_RE = re.compile(LOOKBEHIND_NOT_LETTER + '([ns]?)e-resize')
CURSOR_WEST_RE = re.compile(LOOKBEHIND_NOT_LETTER + '([ns]?)w-resize')

# Matches the condition where we need to replace the horizontal component
# of a background-position value when expressed in horizontal percentage.
# Had to make two regexes because in the case of position-x there is only
# one quantity, and otherwise we don't want to match and change cases with only
# one quantity.
BG_HORIZONTAL_PERCENTAGE_RE = re.compile(r'background(-position)?(%s:%s)'
'([^%%]*?)(%s)%%'
'(%s(?:%s|top|center|bottom))' % (csslex.WHITESPACE,
csslex.WHITESPACE,
csslex.NUM,
csslex.WHITESPACE,
POSSIBLY_NEGATIVE_QUANTITY))

BG_HORIZONTAL_PERCENTAGE_X_RE = re.compile(r'background-position-x(%s:%s)'
'(%s)%%' % (csslex.WHITESPACE,
csslex.WHITESPACE,
csslex.NUM))

# Non-percentage units used for CSS lengths
LENGTH_UNIT = r'(?:em|ex|px|cm|mm|in|pt|pc)'
# To make sure the lone 0 is not just starting a number (like "02") or a percentage like ("0 %")
LOOKAHEAD_END_OF_ZERO = '(?![0-9]|%s%%)' % csslex.WHITESPACE
# A length with a unit specified. Matches "0" too, as it's a length, not a percentage.
LENGTH = '(?:-?%s(?:%s%s)|0+%s)' % (csslex.NUM,
csslex.WHITESPACE,
LENGTH_UNIT,
LOOKAHEAD_END_OF_ZERO)

# Zero length. Used in the replacement functions.
ZERO_LENGTH = re.compile(r'(?:-?0+(?:%s%s)|0+%s)$' % (csslex.WHITESPACE,
LENGTH_UNIT,
LOOKAHEAD_END_OF_ZERO))

# Matches background, background-position, and background-position-x
# properties when using a CSS length for its horizontal positioning.
BG_HORIZONTAL_LENGTH_RE = re.compile(r'background(-position)?(%s:%s)'
'((?:.+?%s+)??)(%s)'
'((?:%s+)(?:%s|top|center|bottom))'% (csslex.WHITESPACE,
csslex.WHITESPACE,
csslex.SPACE,
LENGTH,
csslex.SPACE,
POSSIBLY_NEGATIVE_QUANTITY))

BG_HORIZONTAL_LENGTH_X_RE = re.compile(r'background-position-x(%s:%s)'
'(%s)' % (csslex.WHITESPACE,
csslex.WHITESPACE,
LENGTH))

# Matches the opening of a body selector.
BODY_SELECTOR = r'body%s{%s' % (csslex.WHITESPACE, csslex.WHITESPACE)

# Matches anything up until the closing of a selector.
CHARS_WITHIN_SELECTOR = r'[^\}]*?'

# Matches the direction property in a selector.
DIRECTION_RE = r'direction%s:%s' % (csslex.WHITESPACE, csslex.WHITESPACE)

# These allow us to swap "ltr" with "rtl" and vice versa ONLY within the
# body selector and on the same line.
BODY_DIRECTION_LTR_RE = re.compile(r'(%s)(%s)(%s)(ltr)' %
(BODY_SELECTOR, CHARS_WITHIN_SELECTOR,
DIRECTION_RE),
re.I)
BODY_DIRECTION_RTL_RE = re.compile(r'(%s)(%s)(%s)(rtl)' %
(BODY_SELECTOR, CHARS_WITHIN_SELECTOR,
DIRECTION_RE),
re.I)


# Allows us to swap "direction:ltr" with "direction:rtl" and
# vice versa anywhere in a line.
DIRECTION_LTR_RE = re.compile(r'%s(ltr)' % DIRECTION_RE)
DIRECTION_RTL_RE = re.compile(r'%s(rtl)' % DIRECTION_RE)

# We want to be able to switch left with right and vice versa anywhere
# we encounter left/right strings, EXCEPT inside the background:url(). The next
# two regexes are for that purpose. We have alternate IN_URL versions of the
# regexes compiled in case the user passes the flag that they do
# actually want to have left and right swapped inside of background:urls.
LEFT_RE = re.compile('%s((?:top|bottom)?)(%s)%s%s' % (LOOKBEHIND_NOT_LETTER,
LEFT,
LOOKAHEAD_NOT_CLOSING_PAREN,
LOOKAHEAD_NOT_OPEN_BRACE),
re.I)
RIGHT_RE = re.compile('%s((?:top|bottom)?)(%s)%s%s' % (LOOKBEHIND_NOT_LETTER,
RIGHT,
LOOKAHEAD_NOT_CLOSING_PAREN,
LOOKAHEAD_NOT_OPEN_BRACE),
re.I)
LEFT_IN_URL_RE = re.compile('%s(%s)%s' % (LOOKBEHIND_NOT_LETTER,
LEFT,
LOOKAHEAD_FOR_CLOSING_PAREN),
re.I)
RIGHT_IN_URL_RE = re.compile('%s(%s)%s' % (LOOKBEHIND_NOT_LETTER,
RIGHT,
LOOKAHEAD_FOR_CLOSING_PAREN),
re.I)
LTR_IN_URL_RE = re.compile('%s(%s)%s' % (LOOKBEHIND_NOT_LETTER,
LTR,
LOOKAHEAD_FOR_CLOSING_PAREN),
re.I)
RTL_IN_URL_RE = re.compile('%s(%s)%s' % (LOOKBEHIND_NOT_LETTER,
RTL,
LOOKAHEAD_FOR_CLOSING_PAREN),
re.I)

COMMENT_RE = re.compile('(%s)' % csslex.COMMENT, re.I)

NOFLIP_TOKEN = r'\@noflip'
# The NOFLIP_TOKEN inside of a comment. For now, this requires that comments
# be in the input, which means users of a css compiler would have to run
# this script first if they want this functionality.
NOFLIP_ANNOTATION = r'/\*%s%s%s\*/' % (csslex.WHITESPACE,
NOFLIP_TOKEN,
csslex. WHITESPACE)

# After a NOFLIP_ANNOTATION, and within a class selector, we want to be able
# to set aside a single rule not to be flipped. We can do this by matching
# our NOFLIP annotation and then using a lookahead to make sure there is not
# an opening brace before the match.
NOFLIP_SINGLE_RE = re.compile(r'(%s%s[^;}]+;?)' % (NOFLIP_ANNOTATION,
LOOKAHEAD_NOT_OPEN_BRACE),
re.I)

# After a NOFLIP_ANNOTATION, we want to grab anything up until the next } which
# means the entire following class block. This will prevent all of its
# declarations from being flipped.
NOFLIP_CLASS_RE = re.compile(r'(%s%s})' % (NOFLIP_ANNOTATION,
CHARS_WITHIN_SELECTOR),
re.I)

# border-radis properties and their values
BORDER_RADIUS_TOKENIZER_RE = re.compile(r'((?:%s)?border-radius%s:[^;}]+;?)' % (csslex.IDENT,
csslex.WHITESPACE),
re.I)

# CSS gradients can't be expressed in normal regular expressions, since they
# can contain nested parentheses. So we emulate a re.sub-like function here.

class MatchLike:
def __init__(self, match):
self.match = match

def group(self, group_number):
return self.match


GRADIENT_RE = re.compile(r'%s[\.-]gradient%s\(' % (csslex.IDENT, csslex.WHITESPACE), re.I)

class GradientMatcher:
def sub(self, match_function, input_string):
output = []
m = GRADIENT_RE.search(input_string)
while m:
paren_count = 1
index = m.end(0)
while paren_count > 0:
if input_string[index] == '(':
paren_count += 1
elif input_string[index] == ')':
paren_count -= 1
index += 1
# Here, index would point to the character after the matching closing parenthesis
replacement = match_function(MatchLike(input_string[m.start(0):index]))

output.append(input_string[:m.start(0)] + replacement)
input_string = input_string[index:]
m = GRADIENT_RE.search(input_string)
return ''.join(output)+input_string


class Tokenizer:
"""Replaces any CSS comments with string tokens and vice versa."""

def __init__(self, token_re, token_string):
"""Constructor for the Tokenizer.

Args:
token_re: A regex for the string to be replace by a token.
token_string: The string to put between token delimiters when tokenizing.
"""
logging.debug('Tokenizer::init token_string=%s' % token_string)
self.token_re = token_re
self.token_string = token_string
self.originals = []

def Tokenize(self, line):
"""Replaces any string matching token_re in line with string tokens.

By passing a function as an argument to the re.sub line below, we bypass
the usual rule where re.sub will only replace the left-most occurrence of
a match by calling the passed in function for each occurrence.

Args:
line: A line to replace token_re matches in.

Returns:
line: A line with token_re matches tokenized.
"""
line = self.token_re.sub(self.TokenizeMatches, line)
logging.debug('Tokenizer::Tokenize returns: %s' % line)
return line

def DeTokenize(self, line):
"""Replaces tokens with the original string.

Args:
line: A line with tokens.

Returns:
line with any tokens replaced by the original string.
"""

# Put all of the comments back in by their comment token.
for i, original in enumerate(self.originals):
token = '%s%s_%s%s' % (TOKEN_DELIMITER, self.token_string, i + 1,
TOKEN_DELIMITER)
line = line.replace(token, original)
logging.debug('Tokenizer::DeTokenize i:%s w/%s' % (i, token))
logging.debug('Tokenizer::DeTokenize returns: %s' % line)
return line

def TokenizeMatches(self, m):
"""Replaces matches with tokens and stores the originals.

Args:
m: A match object.

Returns:
A string token which replaces the CSS comment.
"""
logging.debug('Tokenizer::TokenizeMatches %s' % m.group(1))
self.originals.append(m.group(1))
return '%s%s_%s%s' % (TOKEN_DELIMITER,
self.token_string,
len(self.originals),
TOKEN_DELIMITER)


def FixBodyDirectionLtrAndRtl(line):
"""Replaces ltr with rtl and vice versa ONLY in the body direction.

Args:
line: A string to replace instances of ltr with rtl.
Returns:
line with direction: ltr and direction: rtl swapped only in body selector.
line = FixBodyDirectionLtrAndRtl('body { direction:ltr }')
line will now be 'body { direction:rtl }'.
"""

line = BODY_DIRECTION_LTR_RE.sub('\\1\\2\\3%s' % TMP_TOKEN, line)
line = BODY_DIRECTION_RTL_RE.sub('\\1\\2\\3%s' % LTR, line)
line = line.replace(TMP_TOKEN, RTL)
logging.debug('FixBodyDirectionLtrAndRtl returns: %s' % line)
return line


def FixLeftAndRight(line):
"""Replaces left with right and vice versa in line.

Args:
line: A string in which to perform the replacement.

Returns:
line with left and right swapped. For example:
line = FixLeftAndRight('padding-left: 2px; margin-right: 1px;')
line will now be 'padding-right: 2px; margin-left: 1px;'.
"""

line = LEFT_RE.sub("\\1" + TMP_TOKEN, line)
line = RIGHT_RE.sub("\\1" + LEFT, line)
line = line.replace(TMP_TOKEN, RIGHT)
logging.debug('FixLeftAndRight returns: %s' % line)
return line


def FixLeftAndRightInUrl(line):
"""Replaces left with right and vice versa ONLY within background urls.

Args:
line: A string in which to replace left with right and vice versa.

Returns:
line with left and right swapped in the url string. For example:
line = FixLeftAndRightInUrl('background:url(right.png)')
line will now be 'background:url(left.png)'.
"""

line = LEFT_IN_URL_RE.sub(TMP_TOKEN, line)
line = RIGHT_IN_URL_RE.sub(LEFT, line)
line = line.replace(TMP_TOKEN, RIGHT)
logging.debug('FixLeftAndRightInUrl returns: %s' % line)
return line


def FixLtrAndRtlInUrl(line):
"""Replaces ltr with rtl and vice versa ONLY within background urls.

Args:
line: A string in which to replace ltr with rtl and vice versa.

Returns:
line with left and right swapped. For example:
line = FixLtrAndRtlInUrl('background:url(rtl.png)')
line will now be 'background:url(ltr.png)'.
"""

line = LTR_IN_URL_RE.sub(TMP_TOKEN, line)
line = RTL_IN_URL_RE.sub(LTR, line)
line = line.replace(TMP_TOKEN, RTL)
logging.debug('FixLtrAndRtlInUrl returns: %s' % line)
return line


def FixCursorProperties(line):
"""Fixes directional CSS cursor properties.

Args:
line: A string to fix CSS cursor properties in.

Returns:
line reformatted with the cursor properties substituted. For example:
line = FixCursorProperties('cursor: ne-resize')
line will now be 'cursor: nw-resize'.
"""

line = CURSOR_EAST_RE.sub('\\1' + TMP_TOKEN, line)
line = CURSOR_WEST_RE.sub('\\1e-resize', line)
line = line.replace(TMP_TOKEN, 'w-resize')
logging.debug('FixCursorProperties returns: %s' % line)
return line


def FixBorderRadius(line):
"""Fixes border-radius and its browser-specific variants.

Args:
line: A string to fix border-radius in.

Returns:
line reformatted with the border-radius values rearranged. For example:
line = FixBorderRadius('border-radius: 1px 2px 3px 4px / 5px 6px 7px')
line will now be 'border-radius: 2px 1px 4px 3px / 6px 5px 6px 7px'.
"""
line = BORDER_RADIUS_RE.sub(ReorderBorderRadius, line)
logging.debug('FixBorderRadius returns: %s' % line)
return line


def FixFourPartNotation(line):
"""Fixes the second and fourth positions in 4 part CSS notation.

Args:
line: A string to fix 4 part CSS notation in.

Returns:
line reformatted with the 4 part notations swapped. For example:
line = FixFourPartNotation('padding: 1px 2px 3px 4px')
line will now be 'padding: 1px 4px 3px 2px'.
"""
line = FOUR_NOTATION_QUANTITY_RE.sub('\\1 \\4 \\3 \\2', line)
line = FOUR_NOTATION_COLOR_RE.sub('\\1\\2 \\5 \\4 \\3', line)
logging.debug('FixFourPartNotation returns: %s' % line)
return line


def FixBackgroundPosition(line):
"""Fixes horizontal background values in line.

Args:
line: A string to fix horizontal background position values in.

Returns:
line reformatted with the horizontal background values replaced, if possible.
Otherwise, an exception would be raised.
"""
line = BG_HORIZONTAL_PERCENTAGE_RE.sub(CalculateNewBackgroundPosition, line)
line = BG_HORIZONTAL_PERCENTAGE_X_RE.sub(CalculateNewBackgroundPositionX,
line)
line = BG_HORIZONTAL_LENGTH_RE.sub(CalculateNewBackgroundLengthPosition, line)
line = BG_HORIZONTAL_LENGTH_X_RE.sub(CalculateNewBackgroundLengthPositionX,
line)
logging.debug('FixBackgroundPosition returns: %s' % line)
return line


def ReorderBorderRadiusPart(part):
"""Takes a list of zero to four border radius parts and returns a string of them
reordered for bidi mirroring.
"""

# Remove any piece which may be 'None'
part = [piece for piece in part if piece is not None]

if len(part) == 4:
return '%s %s %s %s' % (part[1], part[0], part[3], part[2])
elif len(part) == 3:
return '%s %s %s %s' % (part[1], part[0], part[1], part[2])
elif len(part) == 2:
return '%s %s' % (part[1], part[0])
elif len(part) == 1:
return part[0]
elif len(part) == 0:
return ''
else:
raise Error("This can't happen!")


def ReorderBorderRadius(m):
"""Receives a match object for a border-radius element and reorders it
pieces.
"""
first_group = ReorderBorderRadiusPart(m.groups()[2:6])
second_group = ReorderBorderRadiusPart(m.groups()[6:])
if second_group == '':
return '%sborder-radius%s%s' % (m.group(1),
m.group(2),
first_group)
else:
return '%sborder-radius%s%s / %s' % (m.group(1),
m.group(2),
first_group,
second_group)


def CalculateNewBackgroundPosition(m):
"""Fixes horizontal background-position percentages.

This function should be used as an argument to re.sub since it needs to
perform replacement specific calculations.

Args:
m: A match object.

Returns:
A string with the horizontal background position percentage fixed.
BG_HORIZONTAL_PERCENTAGE_RE.sub(FixBackgroundPosition,
'background-position: 75% 50%')
will return 'background-position: 25% 50%'.
"""

# The flipped value is the offset from 100%
new_x = str(100-int(m.group(4)))

# Since m.group(1) may very well be None type and we need a string..
if m.group(1):
position_string = m.group(1)
else:
position_string = ''

return 'background%s%s%s%s%%%s' % (position_string, m.group(2), m.group(3),
new_x, m.group(5))


def CalculateNewBackgroundPositionX(m):
"""Fixes percent based background-position-x.

This function should be used as an argument to re.sub since it needs to
perform replacement specific calculations.

Args:
m: A match object.

Returns:
A string with the background-position-x percentage fixed.
BG_HORIZONTAL_PERCENTAGE_X_RE.sub(CalculateNewBackgroundPosition,
'background-position-x: 75%')
will return 'background-position-x: 25%'.
"""

# The flipped value is the offset from 100%
new_x = str(100-int(m.group(2)))

return 'background-position-x%s%s%%' % (m.group(1), new_x)

BACKGROUND_POSITION_ERROR_MESSAGE = 'Unmirrorable horizonal value %s: %s\n'

# An exception created for background-position horizontal values set to
# non-zero lengths which makes them unmirrorable.
class BackgroundPositionError(Exception):
def __init__(self, bad_length, whole_value):
self.bad_length = bad_length
self.whole_value = whole_value

def __str__(self):
return BACKGROUND_POSITION_ERROR_MESSAGE % (repr(self.bad_length), self.whole_value)

def WarnForBackgroundPosition(bad_length, whole_value):
if FLAGS['ignore_bad_bgp']:
sys.stderr.write(BACKGROUND_POSITION_ERROR_MESSAGE % (repr(bad_length), whole_value))
else:
raise BackgroundPositionError(bad_length, whole_value)

def CalculateNewBackgroundLengthPosition(m):
"""Fixes horizontal background-position lengths.

This function should be used as an argument to re.sub since it needs to
perform replacement specific calculations.

Args:
m: A match object.

Returns:
A string with the horizontal background position set to 100%, if zero.
Otherwise, an exception will be raised.
BG_HORIZONTAL_LENGTH_RE.sub(CalculateNewBackgroundLengthPosition,
'background-position: 0px 10px')
will return 'background-position: 100% 10px'.
"""

# raise an exception if the length is not zero-valued
if not ZERO_LENGTH.match(m.group(4)):
WarnForBackgroundPosition(m.group(4), m.group(0))
return m.group(0)

# Since m.group(1) may very well be None type and we need a string..
if m.group(1):
position_string = m.group(1)
else:
position_string = ''

return 'background%s%s%s100%%%s' % (position_string, m.group(2), m.group(3),
m.group(5))


def CalculateNewBackgroundLengthPositionX(m):
"""Fixes background-position-x lengths.

This function should be used as an argument to re.sub since it needs to
perform replacement specific calculations.

Args:
m: A match object.

Returns:
A string with the background-position-x set to 100%, if zero.
Otherwiser, an exception will be raised.
BG_HORIZONTAL_LENGTH_X_RE.sub(CalculateNewBackgroundLengthPositionX,
'background-position-x: 0')
will return 'background-position-x: 100%'.
"""

# raise an exception if the length is not zero-valued
if not ZERO_LENGTH.match(m.group(2)):
WarnForBackgroundPosition(m.group(2), m.group(0))
return m.group(0)

return 'background-position-x%s100%%' % m.group(1)


def ChangeLeftToRightToLeft(lines,
swap_ltr_rtl_in_url=None,
swap_left_right_in_url=None):
"""Turns lines into a stream and runs the fixing functions against it.

Args:
lines: An list of CSS lines.
swap_ltr_rtl_in_url: Overrides this flag if param is set.
swap_left_right_in_url: Overrides this flag if param is set.

Returns:
The same lines, but with left and right fixes.
"""

global FLAGS

# Possibly override flags with params.
logging.debug('ChangeLeftToRightToLeft swap_ltr_rtl_in_url=%s, '
'swap_left_right_in_url=%s' % (swap_ltr_rtl_in_url,
swap_left_right_in_url))
if swap_ltr_rtl_in_url is None:
swap_ltr_rtl_in_url = FLAGS['swap_ltr_rtl_in_url']
if swap_left_right_in_url is None:
swap_left_right_in_url = FLAGS['swap_left_right_in_url']

# Turns the array of lines into a single line stream.
logging.debug('LINES COUNT: %s' % len(lines))
line = TOKEN_LINES.join(lines)

# Tokenize any single line rules with the /* noflip */ annotation.
noflip_single_tokenizer = Tokenizer(NOFLIP_SINGLE_RE, 'NOFLIP_SINGLE')
line = noflip_single_tokenizer.Tokenize(line)

# Tokenize any class rules with the /* noflip */ annotation.
noflip_class_tokenizer = Tokenizer(NOFLIP_CLASS_RE, 'NOFLIP_CLASS')
line = noflip_class_tokenizer.Tokenize(line)

# Tokenize the comments so we can preserve them through the changes.
comment_tokenizer = Tokenizer(COMMENT_RE, 'C')
line = comment_tokenizer.Tokenize(line)

# Tokenize gradients since we don't want to mirror the values inside
gradient_tokenizer = Tokenizer(GradientMatcher(), 'GRADIENT')
line = gradient_tokenizer.Tokenize(line)

# Here starteth the various left/right orientation fixes.
line = FixBodyDirectionLtrAndRtl(line)

if swap_left_right_in_url:
line = FixLeftAndRightInUrl(line)

if swap_ltr_rtl_in_url:
line = FixLtrAndRtlInUrl(line)

line = FixLeftAndRight(line)
line = FixCursorProperties(line)

line = FixBorderRadius(line)
# Since FourPartNotation conflicts with BorderRadius, we tokenize border-radius properties here.
border_radius_tokenizer = Tokenizer(BORDER_RADIUS_TOKENIZER_RE, 'BORDER_RADIUS')
line = border_radius_tokenizer.Tokenize(line)
line = FixFourPartNotation(line)
line = border_radius_tokenizer.DeTokenize(line)

line = FixBackgroundPosition(line)

line = gradient_tokenizer.DeTokenize(line)

# DeTokenize the single line noflips.
line = noflip_single_tokenizer.DeTokenize(line)

# DeTokenize the class-level noflips.
line = noflip_class_tokenizer.DeTokenize(line)

# DeTokenize the comments.
line = comment_tokenizer.DeTokenize(line)

# Rejoin the lines back together.
lines = line.split(TOKEN_LINES)

return lines

def usage():
"""Prints out usage information."""

print 'Usage:'
print ' ./cssjanus.py < file.css > file-rtl.css'
print 'Flags:'
print ' --swap_left_right_in_url: Fixes "left"/"right" string within urls.'
print ' Ex: ./cssjanus.py --swap_left_right_in_url < file.css > file_rtl.css'
print ' --swap_ltr_rtl_in_url: Fixes "ltr"/"rtl" string within urls.'
print ' Ex: ./cssjanus.py --swap_ltr_rtl_in_url < file.css > file_rtl.css'
print ' --ignore_bad_bgp: Ignores unmirrorable background-position values.'
print ' Ex: ./cssjanus.py --ignore_bad_bgp < file.css > file_rtl.css'

def setflags(opts):
"""Parse the passed in command line arguments and set the FLAGS global.

Args:
opts: getopt iterable intercepted from argv.
"""

global FLAGS

# Parse the arguments.
for opt, arg in opts:
logging.debug('opt: %s, arg: %s' % (opt, arg))
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-d", "--debug"):
logging.getLogger().setLevel(logging.DEBUG)
elif opt == '--swap_ltr_rtl_in_url':
FLAGS['swap_ltr_rtl_in_url'] = True
elif opt == '--swap_left_right_in_url':
FLAGS['swap_left_right_in_url'] = True
elif opt == '--ignore_bad_bgp':
FLAGS['ignore_bad_bgp'] = True


def main(argv):
"""Sends stdin lines to ChangeLeftToRightToLeft and writes to stdout."""

# Define the flags.
try:
opts, args = getopt.getopt(argv, 'hd', ['help', 'debug',
'swap_left_right_in_url',
'swap_ltr_rtl_in_url',
'ignore_bad_bgp'])
except getopt.GetoptError:
usage()
sys.exit(2)

# Parse and set the flags.
setflags(opts)

# Call the main routine with all our functionality.
fixed_lines = ChangeLeftToRightToLeft(sys.stdin.readlines())
sys.stdout.write(''.join(fixed_lines))

if __name__ == '__main__':
main(sys.argv[1:])

Change log

r30 by ebryon77 on Jan 4, 2012   Diff
Modifying CSSJanus to include
functionality for swapping CSS3 Border
Radius and Gradients. Also added exception
if unmirrorable background-positions are
encountered; can be overridden using the
--ignore_bad_bgp flag
Go to: 
Project members, sign in to write a code review

Older revisions

r27 by elsigh on Jun 30, 2009   Diff
fixes #9
r26 by elsigh on Jun 24, 2009   Diff
http://code.google.com/p/cssjanus/issu
es/detail?id=3&can=1 fixed
r19 by elsigh on Apr 14, 2008   Diff
Updating for use with Google App
Engine, as well as adding line-level
@noflip capabilities.
All revisions of this file

File info

Size: 32049 bytes, 820 lines

File properties

svn:executable
*
Powered by Google Project Hosting