1# Copyright 2014 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Process Chrome resources (HTML/CSS/JS) to handle <include> and <if> tags."""
6
7from collections import defaultdict
8import re
9import os
10
11
12class LineNumber(object):
13  """A simple wrapper to hold line information (e.g. file.js:32).
14
15  Args:
16    source_file: A file path.
17    line_number: The line in |file|.
18  """
19  def __init__(self, source_file, line_number):
20    self.file = source_file
21    self.line_number = int(line_number)
22
23
24class FileCache(object):
25  """An in-memory cache to speed up reading the same files over and over.
26
27  Usage:
28      FileCache.read(path_to_file)
29  """
30
31  _cache = defaultdict(str)
32
33  @classmethod
34  def read(self, source_file):
35    """Read a file and return it as a string.
36
37    Args:
38        source_file: a file to read and return the contents of.
39
40    Returns:
41        |file| as a string.
42    """
43    abs_file = os.path.abspath(source_file)
44    self._cache[abs_file] = self._cache[abs_file] or open(abs_file, "r").read()
45    return self._cache[abs_file]
46
47
48class Processor(object):
49  """Processes resource files, inlining the contents of <include> tags, removing
50  <if> tags, and retaining original line info.
51
52  For example
53
54      1: /* blah.js */
55      2: <if expr="is_win">
56      3: <include src="win.js">
57      4: </if>
58
59  would be turned into:
60
61      1: /* blah.js */
62      2:
63      3: /* win.js */
64      4: alert('Ew; Windows.');
65      5:
66
67  Args:
68      source_file: A file to process.
69
70  Attributes:
71      contents: Expanded contents after inlining <include>s and stripping <if>s.
72      included_files: A list of files that were inlined via <include>.
73  """
74
75  _IF_TAGS_REG = "</?if[^>]*?>"
76  _INCLUDE_REG = "<include[^>]+src=['\"]([^>]*)['\"]>"
77
78  def __init__(self, source_file):
79    self._included_files = set()
80    self._index = 0
81    self._lines = self._get_file(source_file)
82
83    while self._index < len(self._lines):
84      current_line = self._lines[self._index]
85      match = re.search(self._INCLUDE_REG, current_line[2])
86      if match:
87        file_dir = os.path.dirname(current_line[0])
88        self._include_file(os.path.join(file_dir, match.group(1)))
89      else:
90        self._index += 1
91
92    for i, line in enumerate(self._lines):
93      self._lines[i] = line[:2] + (re.sub(self._IF_TAGS_REG, "", line[2]),)
94
95    self.contents = "\n".join(l[2] for l in self._lines)
96
97  # Returns a list of tuples in the format: (file, line number, line contents).
98  def _get_file(self, source_file):
99    lines = FileCache.read(source_file).splitlines()
100    return [(source_file, lnum + 1, line) for lnum, line in enumerate(lines)]
101
102  def _include_file(self, source_file):
103    self._included_files.add(source_file)
104    f = self._get_file(source_file)
105    self._lines = self._lines[:self._index] + f + self._lines[self._index + 1:]
106
107  def get_file_from_line(self, line_number):
108    """Get the original file and line number for an expanded file's line number.
109
110    Args:
111        line_number: A processed file's line number.
112    """
113    line_number = int(line_number) - 1
114    return LineNumber(self._lines[line_number][0], self._lines[line_number][1])
115
116  @property
117  def included_files(self):
118    """A list of files that were inlined via <include>."""
119    return self._included_files
120