diff --git a/giscanner/docmain.py b/giscanner/docmain.py
index dab063ef2ead1f91b262eca1feab30936aba5d74..88430f05819a2133c488a2b736bcc9c2cf691b5e 100644
--- a/giscanner/docmain.py
+++ b/giscanner/docmain.py
@@ -51,6 +51,8 @@ def doc_main(args):
     parser.add_argument("-s", "--write-sections-file",
                         action="store_const", dest="format", const="sections",
                         help="Backwards-compatible equivalent to -f sections")
+    parser.add_argument("--templates-dir",
+                        action="store")
 
     args = parser.parse_args(args[1:])
     if not args.output:
@@ -74,7 +76,7 @@ def doc_main(args):
         with open(args.output, 'w', encoding='utf-8') as fp:
             write_sections_file(fp, sections_file)
     else:
-        writer = DocWriter(transformer, args.language, args.format)
+        writer = DocWriter(transformer, args.language, args.format, args.templates_dir)
         writer.write(args.output)
 
     return 0
diff --git a/giscanner/docwriter.py b/giscanner/docwriter.py
index d0cd610f2f343fd1263532127fdd3994cdeb059c..b72ab2ac9bc6a036623c996f6b96eeacf820b83a 100644
--- a/giscanner/docwriter.py
+++ b/giscanner/docwriter.py
@@ -1288,7 +1288,7 @@ LANGUAGES = {
 
 
 class DocWriter(object):
-    def __init__(self, transformer, language, output_format):
+    def __init__(self, transformer, language, output_format, templates_dir=None):
         self._transformer = transformer
 
         try:
@@ -1300,18 +1300,20 @@ class DocWriter(object):
         self._formatter = formatter_class(self._transformer)
         self._language = self._formatter.language
         self._output_format = output_format
+        self._templates_dir = templates_dir
 
         self._lookup = self._get_template_lookup()
 
     def _get_template_lookup(self):
-        if 'UNINSTALLED_INTROSPECTION_SRCDIR' in os.environ:
+        if self._templates_dir is not None:
+            srcdir = self._templates_dir
+        elif 'UNINSTALLED_INTROSPECTION_SRCDIR' in os.environ:
             top_srcdir = os.environ['UNINSTALLED_INTROSPECTION_SRCDIR']
-            srcdir = os.path.join(top_srcdir, 'giscanner')
+            srcdir = os.path.join(top_srcdir, 'giscanner', 'doctemplates')
         else:
-            srcdir = os.path.dirname(__file__)
+            srcdir = os.path.join(os.path.dirname(__file__), 'doctemplates')
 
-        template_dir = os.path.join(srcdir, 'doctemplates',
-                                    self._formatter.output_format)
+        template_dir = os.path.join(srcdir, self._formatter.output_format)
 
         return TemplateLookup(directories=[template_dir],
                               module_directory=tempfile.mkdtemp(),

