# -*- coding: utf-8 -*-

import sys
import os
import json

import requests
from deconstrst.builder import DeconstJSONBuilder
from sphinx.application import Sphinx
from sphinx.builders import BUILTIN_BUILDERS


def build(srcdir, destdir):
    """
    Invoke Sphinx with locked arguments to generate JSON content.
    """

    # I am a terrible person
    BUILTIN_BUILDERS['deconst'] = DeconstJSONBuilder

    doctreedir = os.path.join(destdir, '.doctrees')

    app = Sphinx(srcdir=srcdir, confdir=srcdir, outdir=destdir,
                 doctreedir=doctreedir, buildername="deconst",
                 confoverrides={}, status=sys.stdout, warning=sys.stderr,
                 freshenv=True, warningiserror=False, tags=[], verbosity=0,
                 parallel=1)
    app.build(True, [])

    return app.statuscode


def submit(destdir, content_store_url, content_id_base):
    """
    Submit the generated json files to the content store API.
    """

    headers = {
        "Content-Type": "application/json"
    }

    for (dirpath, dirnames, filenames) in os.walk(destdir):
        for name in filenames:
            fullpath = os.path.join(dirpath, name)
            ext = os.path.splitext(name)[1]

            if os.path.isfile(fullpath) and ext == ".json":
                relpath = os.path.relpath(fullpath, destdir)

                print("submitting [{}] ... ".format(relpath), end='')

                payload = dict(id=content_id_base + relpath)

                with open(fullpath, "r") as inf:
                    payload["body"] = json.load(inf)

                response = requests.put(content_store_url + "content",
                                        data=json.dumps(payload),
                                        headers=headers)
                response.raise_for_status()
                print("success")

    print("All generated content submitted to the content store.")

    return 0

@@ -1164,16 +1164,22 @@
         
+ base,
  ext = o
@@ -1203,11 +1203,8 @@
 ame)
-%5B1%5D
 %0A%0A  
@@ -1339,22 +1339,231 @@
         
-print(
+content_suffix = os.path.relpath(%0A                    os.path.join(dirpath, base), destdir%0A                )%0A                content_id = content_id_base + content_suffix%0A%0A                print(%0A                    
 %22submitt
@@ -1570,16 +1570,24 @@
 ing %5B%7B%7D%5D
+ as %5B%7B%7D%5D
  ... %22.f
@@ -1603,17 +1603,66 @@
 path
-), end=''
+, content_id),%0A                    end=''%0A                
 )%0A%0A 
@@ -1708,23 +1708,8 @@
 t_id
-_base + relpath
 )%0A%0A 

Correct the content ID generation.