Repository: getnikola/nikola

Issue:
listing directive explodes badly if used wrong
Example: 

```
.. listing:: hello.py
```

Which is a way too common first attempt to use it, crashes like this:

```
TaskError - taskid:render_posts:cache/posts/foo.html
PythonAction Error
Traceback (most recent call last):
  File "/home/ralsina/.virtualenvs/nikola/local/lib/python2.7/site-packages/doit/action.py", line 383, in execute
    returned_value = self.py_callable(*self.args, **kwargs)
  File "/home/ralsina/Desktop/proyectos/nikola/master/nikola/post.py", line 485, in compile
    self.is_two_file),
  File "/home/ralsina/Desktop/proyectos/nikola/master/nikola/plugins/compile/rest/__init__.py", line 100, in compile_html
    output, error_level, deps = self.compile_html_string(data, source, is_two_file)
  File "/home/ralsina/Desktop/proyectos/nikola/master/nikola/plugins/compile/rest/__init__.py", line 86, in compile_html_string
    }, logger=self.logger, source_path=source_path, l_add_ln=add_ln, transforms=self.site.rst_transforms)
  File "/home/ralsina/Desktop/proyectos/nikola/master/nikola/plugins/compile/rest/__init__.py", line 276, in rst2html
    pub.publish(enable_exit_status=enable_exit_status)
  File "/home/ralsina/.virtualenvs/nikola/local/lib/python2.7/site-packages/docutils-0.12-py2.7.egg/docutils/core.py", line 217, in publish
    self.settings)
  File "/home/ralsina/.virtualenvs/nikola/local/lib/python2.7/site-packages/docutils-0.12-py2.7.egg/docutils/readers/__init__.py", line 72, in read
    self.parse()
  File "/home/ralsina/.virtualenvs/nikola/local/lib/python2.7/site-packages/docutils-0.12-py2.7.egg/docutils/readers/__init__.py", line 78, in parse
    self.parser.parse(self.input, document)
  File "/home/ralsina/.virtualenvs/nikola/local/lib/python2.7/site-packages/docutils-0.12-py2.7.egg/docutils/parsers/rst/__init__.py", line 172, in parse
    self.statemachine.run(inputlines, document, inliner=self.inliner)
  File "/home/ralsina/.virtualenvs/nikola/local/lib/python2.7/site-packages/docutils-0.12-py2.7.egg/docutils/parsers/rst/states.py", line 170, in run
    input_source=document['source'])
  File "/home/ralsina/.virtualenvs/nikola/local/lib/python2.7/site-packages/docutils-0.12-py2.7.egg/docutils/statemachine.py", line 239, in run
    context, state, transitions)
  File "/home/ralsina/.virtualenvs/nikola/local/lib/python2.7/site-packages/docutils-0.12-py2.7.egg/docutils/statemachine.py", line 460, in check_line
    return method(match, context, next_state)
  File "/home/ralsina/.virtualenvs/nikola/local/lib/python2.7/site-packages/docutils-0.12-py2.7.egg/docutils/parsers/rst/states.py", line 2299, in explicit_markup
    nodelist, blank_finish = self.explicit_construct(match)
  File "/home/ralsina/.virtualenvs/nikola/local/lib/python2.7/site-packages/docutils-0.12-py2.7.egg/docutils/parsers/rst/states.py", line 2311, in explicit_construct
    return method(self, expmatch)
  File "/home/ralsina/.virtualenvs/nikola/local/lib/python2.7/site-packages/docutils-0.12-py2.7.egg/docutils/parsers/rst/states.py", line 2054, in directive
    directive_class, match, type_name, option_presets)
  File "/home/ralsina/.virtualenvs/nikola/local/lib/python2.7/site-packages/docutils-0.12-py2.7.egg/docutils/parsers/rst/states.py", line 2103, in run_directive
    result = directive_instance.run()
  File "/home/ralsina/Desktop/proyectos/nikola/master/nikola/plugins/compile/rest/listing.py", line 174, in run
    lang = self.arguments.pop(0)
IndexError: pop from empty list
```



Discussion:

Resolving patch:
diff --git a/nikola/plugins/compile/rest/listing.py b/nikola/plugins/compile/rest/listing.py
--- a/nikola/plugins/compile/rest/listing.py
+++ b/nikola/plugins/compile/rest/listing.py
@@ -171,7 +171,12 @@ def run(self):
         """Run listing directive."""
         _fname = self.arguments.pop(0)
         fname = _fname.replace('/', os.sep)
-        lang = self.arguments.pop(0)
+        try:
+            lang = self.arguments.pop(0)
+            self.options['code'] = lang
+        except IndexError:
+            self.options['literal'] = True
+
         if len(self.folders) == 1:
             listings_folder = next(iter(self.folders.keys()))
             if fname.startswith(listings_folder):
@@ -181,7 +186,6 @@ def run(self):
         else:
             fpath = os.path.join(fname)  # must be new syntax: specify folder name
         self.arguments.insert(0, fpath)
-        self.options['code'] = lang
         if 'linenos' in self.options:
             self.options['number-lines'] = self.options['linenos']
         with io.open(fpath, 'r+', encoding='utf8') as fileobject:


Test patch: