Permalink
Browse files

Now properly parses a PHP file

  • Loading branch information...
1 parent c8906cb commit 1bfaaf65b97f9fff6dc94c20fa8d403f5f5a9ed6 @dafyddcrosby committed Apr 25, 2012
Showing with 32 additions and 5 deletions.
  1. +12 −5 extract.py
  2. +17 −0 tests/data/php_1.php
  3. +3 −0 tests/get_gettext_strings.py
View
@@ -8,9 +8,6 @@
USAGE:
extract.py [files]
-TODO:
-cleaner searching of gettext strings
-
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
@@ -32,10 +29,16 @@
import sys
-DEBUG = True
+DEBUG = False
KEYWORD = "_"
def check_file(filepath):
+ """
+ This goes through the PHP file and extracts the heredoc strings so that
+ they can be searched for gettext strings.
+
+ TODO: Ensure it's getting heredoc strings from inside PHP block
+ """
try:
handle = open(filepath, "r")
except IOError, err:
@@ -64,15 +67,19 @@ def check_file(filepath):
inheredoc = False
if DEBUG:
print('exiting heredoc word: ' + heredoc_word)
- gettext_strings.join(search_heredoc_string(heredoc_string))
+ print('heredoc string is: ' + heredoc_string)
+ gettext_strings.extend(search_heredoc_string(heredoc_string))
heredoc_string = ""
else:
heredoc_string.join(line)
+ return gettext_strings
def search_heredoc_string(string):
"""
Go through the heredoc string itself and search for
gettext strings, returning an array
+
+ TODO: Make this less ugly
"""
gettext_strings = []
index = 0
View
@@ -0,0 +1,17 @@
+<?php
+
+echo "this shouldn't be {$_('parsed')}";
+
+$blue =<<<BLUE
+This should get {$_('blue')}
+BLUE
+
+?>
+None of this should be {$_('parsed either')}
+<?php
+
+echo <<<TEST
+But this part should be parsed, such as {$_('red')}
+and {$_('green')}
+TEST
+?>
@@ -18,6 +18,9 @@ def test_mixed_gettext_and_plaintext(self):
def test_multiple_gettext_and_plaintext(self):
self.assertEqual(extract.search_heredoc_string("{$_('foo')} {$_('yo')} bar"), ['foo', 'yo'])
+class TestParsingPHPFile(unittest.TestCase):
+ def test_php_1(self):
+ self.assertEqual(extract.check_file('data/php_1.php'), ['blue', 'red', 'green'])
if __name__ == '__main__':
unittest.main()

0 comments on commit 1bfaaf6

Please sign in to comment.