Adjust how exporting works to always use 'index.md' for files.

The older version of the export scripts would try to reduce
the number of directories we created and, so we would only
put a markdown file in a separate directory if it was the parent
of other files (e.g., /Home would be exported to
/Home/index.md, but /Home/chromecompatfaq would be exported to
/Home/chromecompatfaq.md).

This could result in multiple files being in a single directory,
which is nice as long as the files aren't referencing other
assets like images; if you add a foo.png next to the md file,
it's not clear which file references foo.png.

And, it turns out that Eleventy by default moves things around anyway
so that /Home/chromecompatfaq.md would get compiled to
/Home/chromecompatfaq/index.html.

So, this CL changes the export so that every file gets its own
directory. This is something we can change down the road, of course,
but at least this makes the initial mapping from Sites URL to file
location straightforward.

Change-Id: I4a7ecb3cbaab25236844672e44cf2911b70b9b25
Reviewed-on: https://chromium-review.googlesource.com/c/website/+/3260348
Commit-Queue: Dirk Pranke <dpranke@google.com>
Reviewed-by: Struan Shrimpton <sshrimp@google.com>
diff --git a/scripts/common.py b/scripts/common.py
index 864f5bf..f5c8e3d 100644
--- a/scripts/common.py
+++ b/scripts/common.py
@@ -24,9 +24,9 @@
 
 site = 'https://www.chromium.org'
 
-REPO_DIR = os.path.dirname(os.path.dirname(__file__))
-SOURCE_DIR = 'site'
-BUILD_DIR = 'build'
+REPO_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
+SITE_DIR = os.path.join(REPO_DIR, 'site')
+BUILD_DIR = os.path.join(REPO_DIR, 'build')
 DEFAULT_TEMPLATE = '/_includes/page.html'
 
 
@@ -58,6 +58,10 @@
     with open(path, 'wb') as fp:
         return fp.write(content)
 
+def write_text_file(path, content):
+    with open(path, 'w') as fp:
+        return fp.write(content)
+
 def read_paths(path):
     paths = set()
     with open(path) as fp:
@@ -71,7 +75,7 @@
     return paths
 
 
-def to_path(page, top=SOURCE_DIR, ext='.md'):
+def to_path(page, top=SITE_DIR, ext='.md'):
     page = page.strip()
     if page == '/':
         page = ''
@@ -100,7 +104,9 @@
     return sorted(paths)
 
 
-def write_if_changed(path, content):
+def write_if_changed(path, content, mode='wb', encoding='utf-8'):
+    if mode == 'w':
+        content = content.encode(encoding)
     os.makedirs(os.path.dirname(path), exist_ok=True)
     if os.path.exists(path):
         with open(path, 'rb') as fp: