#compdef nikola

_nikola() {
    local -a commands tasks
    # format is 'completion:description'
    commands=(
        'help: show help'
        'install_plugin: install plugin into current site'
        'check: check links and files in the generated site'
        'forget: clear successful run status from internal DB'
        'import_wordpress: import a WordPress dump'
        'console: start an interactive Python console with access to your site'
        'mincss: apply mincss to the generated site'
        'init: create a Nikola site in the specified folder'
        'version: print the Nikola version number'
        'build: run tasks'
        'tabcompletion: generate script for tab-complention'
        'bootswatch_theme: given a swatch name from bootswatch.com and a parent theme, creates a custom theme'
        'new_post: create a new blog post or site page'
        'doit_auto: automatically execute tasks when a dependency changes'
        'dumpdb: dump dependency DB'
        'run: run tasks'
        'deploy: deploy the site'
        'auto: automatically detect site changes, rebuild and optionally refresh a browser'
        'serve: start the test webserver'
        'import_blogger: import a blogger dump'
        'install_theme: install theme into current site'
        'install_bs3: install bootstrap3 into the current site'
        'strace: use strace to list file_deps and targets'
        'orphans: list all orphans'
        'list: list tasks from dodo file'
        'ignore: ignore task (skip) on subsequent runs'
        'import_feed: import a RSS/Atom dump'
        'clean: clean action / remove targets'
    )

    # split output by lines to create an array
    tasks=(
'render_posts: Build HTML fragments from metadata and text.'
'render_tags: Render the tag/category pages and feeds.'
'render_sources: Copy page sources into the output.'
'redirect: Generate redirections'
'generate_rss: Generate RSS feeds.'
'render_pages: Render pages into output.'
'copy_files: Copy static files into the output folder.'
'build_sass: Generate CSS out of Sass sources.'
'copy_assets: Copy theme assets into output.'
'render_archive: Render the post archives.'
'render_galleries: Render image galleries.'
'render_indexes: Render the blog indexes.'
'render_site: Group of tasks to render the site.'
'build_less: Generate CSS out of LESS sources.'
'render_listings: Render pretty listings.'
'create_bundles: Bundle assets using WebAssets.'
'post_render: Group of tasks to be executes after site is rendered.'
'sitemap: Generate google sitemap.'
)

    # complete command or task name
    if (( CURRENT == 2 )); then
        _arguments -A : '::cmd:(($commands))' '::task:(($tasks))'
        return
    fi

    # revome program name from $words and decrement CURRENT
    local curcontext context state state_desc line
    _arguments -C '*:: :->'

    # complete sub-command or task options
    local -a _command_args
    case "$words[1]" in
        
      (help)
          _command_args=(
            '--db-file[file used to save successful runs]' \
            '--backend[Select dependency file backend.Available options dbm, json, sqlite3. [default: %(default)s\]]' \
            '*::task:(($tasks))'
            '::cmd:(($commands))'
            ''
        )
      ;;


      (install_plugin)
          _command_args=(
            '(-l|--list)'{-l,--list}'[Show list of available plugins.]' \
            '(-u|--url)'{-u,--url}'[URL for the plugin repository (default: http://plugins.getnikola.com/v6/plugins.json)]' \
            ''
        )
      ;;


      (check)
          _command_args=(
            '(-l|--check-links)'{-l,--check-links}'[Check for dangling links]' \
            '(-f|--check-files)'{-f,--check-files}'[Check for unknown (orphaned and not generated) files]' \
            '--clean-files[Remove all unknown files, use with caution]' \
            '--find-sources[List possible source files for files with broken links.]' \
            ''
        )
      ;;


      (forget)
          _command_args=(
            '--db-file[file used to save successful runs]' \
            '--backend[Select dependency file backend.Available options dbm, json, sqlite3. [default: %(default)s\]]' \
            '(-s|--follow-sub)'{-s,--follow-sub}'[forget task dependencies too]' \
            '*::task:(($tasks))'
            ''
        )
      ;;


      (import_wordpress)
          _command_args=(
            '(-o|--output-folder)'{-o,--output-folder}'[Location to write imported content.]' \
            '(-d|--no-drafts)'{-d,--no-drafts}'[Don't import drafts]' \
            '--squash-newlines[Shorten multiple newlines in a row to only two newlines]' \
            '--no-downloads[Do not try to download files for the import]' \
            ''
        )
      ;;


      (console)
          _command_args=(
            '(-p|--plain)'{-p,--plain}'[Force the plain Python console]' \
            ''
        )
      ;;


      (mincss)
          _command_args=(
            
            ''
        )
      ;;


      (init)
          _command_args=(
            '--demo[Create a site filled with example data.]' \
            ''
        )
      ;;


      (version)
          _command_args=(
            
            ''
        )
      ;;


      (build)
          _command_args=(
            '--db-file[file used to save successful runs]' \
            '--backend[Select dependency file backend.Available options dbm, json, sqlite3. [default: %(default)s\]]' \
            '(-a|--always-execute)'{-a,--always-execute}'[always execute tasks even if up-to-date [default: %(default)s\]]' \
            '(-c|--continue)'{-c,--continue}'[continue executing tasks even after a failure [default: %(default)s\]]' \
            '(-v|--verbosity)'{-v,--verbosity}'[0 capture (do not print) stdout/stderr from task. 1 capture stdout only. 2 do not capture anything (print everything immediately). [default: 1\]]' \
            '(-r|--reporter)'{-r,--reporter}'[Choose output reporter. Available: 'default': report output on console 'executed-only': no output for skipped (up-to-date) and group tasks 'json': output result in json format [default: %(default)s\] ]' \
            '(-o|--output-file)'{-o,--output-file}'[write output into file [default: stdout\]]' \
            '(-n|--process)'{-n,--process}'[number of subprocesses[default: %(default)s\]]' \
            '(-P|--parallel-type)'{-P,--parallel-type}'[Tasks can be executed in parallel in different ways: 'process': uses python multiprocessing module 'thread': uses threads [default: %(default)s\] ]' \
            '--strict[Fail on things that would normally be warnings.]' \
            '(-q|--quiet)'{-q,--quiet}'[Run quietly.]' \
            '*::task:(($tasks))'
            ''
        )
      ;;


      (tabcompletion)
          _command_args=(
            '--db-file[file used to save successful runs]' \
            '--backend[Select dependency file backend.Available options dbm, json, sqlite3. [default: %(default)s\]]' \
            '(-s|--shell)'{-s,--shell}'[Completion code for SHELL. default: "bash". options: [bash, zsh\]]' \
            '--hardcode-tasks[Hardcode tasks from current task list.]' \
            ''
        )
      ;;


      (bootswatch_theme)
          _command_args=(
            '(-n|--name)'{-n,--name}'[New theme name (default: custom)]' \
            '-s[Name of the swatch from bootswatch.com.]' \
            '(-p|--parent)'{-p,--parent}'[Parent theme name (default: bootstrap3)]' \
            ''
        )
      ;;


      (new_post)
          _command_args=(
            '(-p|--page)'{-p,--page}'[Create a page instead of a blog post.]' \
            '(-t|--title)'{-t,--title}'[Title for the page/post.]' \
            '--tags[Comma-separated tags for the page/post.]' \
            '-1[Create post with embedded metadata (single file format)]' \
            '-2[Create post with separate metadata (two file format)]' \
            '(-f|--format)'{-f,--format}'[Markup format for post, one of rest, markdown, wiki, bbcode, html, textile, txt2tags]' \
            '-s[Schedule post based on recurrence rule]' \
            ''
        )
      ;;


      (doit_auto)
          _command_args=(
            '--db-file[file used to save successful runs]' \
            '--backend[Select dependency file backend.Available options dbm, json, sqlite3. [default: %(default)s\]]' \
            '(-v|--verbosity)'{-v,--verbosity}'[0 capture (do not print) stdout/stderr from task. 1 capture stdout only. 2 do not capture anything (print everything immediately). [default: 1\]]' \
            
            '*::task:(($tasks))'
            ''
        )
      ;;


      (dumpdb)
          _command_args=(
            '--db-file[file used to save successful runs]' \
            ''
        )
      ;;


      (run)
          _command_args=(
            '--db-file[file used to save successful runs]' \
            '--backend[Select dependency file backend.Available options dbm, json, sqlite3. [default: %(default)s\]]' \
            '(-a|--always-execute)'{-a,--always-execute}'[always execute tasks even if up-to-date [default: %(default)s\]]' \
            '(-c|--continue)'{-c,--continue}'[continue executing tasks even after a failure [default: %(default)s\]]' \
            '(-v|--verbosity)'{-v,--verbosity}'[0 capture (do not print) stdout/stderr from task. 1 capture stdout only. 2 do not capture anything (print everything immediately). [default: 1\]]' \
            '(-r|--reporter)'{-r,--reporter}'[Choose output reporter. Available: 'default': report output on console 'executed-only': no output for skipped (up-to-date) and group tasks 'json': output result in json format [default: %(default)s\] ]' \
            '(-o|--output-file)'{-o,--output-file}'[write output into file [default: stdout\]]' \
            '(-n|--process)'{-n,--process}'[number of subprocesses[default: %(default)s\]]' \
            '(-P|--parallel-type)'{-P,--parallel-type}'[Tasks can be executed in parallel in different ways: 'process': uses python multiprocessing module 'thread': uses threads [default: %(default)s\] ]' \
            '*::task:(($tasks))'
            ''
        )
      ;;


      (deploy)
          _command_args=(
            
            ''
        )
      ;;


      (auto)
          _command_args=(
            '-b[Start a web browser.]' \
            '(-p|--port)'{-p,--port}'[Port nummber (default: 8000)]' \
            ''
        )
      ;;


      (serve)
          _command_args=(
            '(-p|--port)'{-p,--port}'[Port nummber (default: 8000)]' \
            '(-a|----address)'{-a,----address}'[Address to bind (default: 127.0.0.1)]' \
            ''
        )
      ;;


      (import_blogger)
          _command_args=(
            '(-o|--output-folder)'{-o,--output-folder}'[Location to write imported content.]' \
            '(-d|--no-drafts)'{-d,--no-drafts}'[Don't import drafts]' \
            ''
        )
      ;;


      (install_theme)
          _command_args=(
            '(-l|--list)'{-l,--list}'[Show list of available themes.]' \
            '(-u|--url)'{-u,--url}'[URL for the theme repository (default: http://themes.getnikola.com/v6/themes.json)]' \
            ''
        )
      ;;


      (install_bs3)
          _command_args=(
            
            ''
        )
      ;;


      (strace)
          _command_args=(
            '--db-file[file used to save successful runs]' \
            '--backend[Select dependency file backend.Available options dbm, json, sqlite3. [default: %(default)s\]]' \
            '(-a|--all)'{-a,--all}'[display all files (not only from within CWD path)]' \
            '(-k|--keep)'{-k,--keep}'[save strace command output into strace.txt]' \
            '*::task:(($tasks))'
            ''
        )
      ;;


      (orphans)
          _command_args=(
            
            ''
        )
      ;;


      (list)
          _command_args=(
            '--db-file[file used to save successful runs]' \
            '--backend[Select dependency file backend.Available options dbm, json, sqlite3. [default: %(default)s\]]' \
            '--all[list include all sub-tasks from dodo file]' \
            '(-q|--quiet)'{-q,--quiet}'[print just task name (less verbose than default)]' \
            '(-s|--status)'{-s,--status}'[print task status (R)un, (U)p-to-date, (I)gnored]' \
            '(-p|--private)'{-p,--private}'[print private tasks (start with '_')]' \
            '--deps[print list of dependencies (file dependencies only)]' \
            '--template[display entries with template]' \
            '*::task:(($tasks))'
            ''
        )
      ;;


      (ignore)
          _command_args=(
            '--db-file[file used to save successful runs]' \
            '--backend[Select dependency file backend.Available options dbm, json, sqlite3. [default: %(default)s\]]' \
            '*::task:(($tasks))'
            ''
        )
      ;;


      (import_feed)
          _command_args=(
            '(-o|--output-folder)'{-o,--output-folder}'[Location to write imported content.]' \
            ''
        )
      ;;


      (clean)
          _command_args=(
            '--db-file[file used to save successful runs]' \
            '--backend[Select dependency file backend.Available options dbm, json, sqlite3. [default: %(default)s\]]' \
            '(-c|--clean-dep)'{-c,--clean-dep}'[clean task dependencies too]' \
            '(-a|--clean-all)'{-a,--clean-all}'[clean all task]' \
            '(-n|--dry-run)'{-n,--dry-run}'[print actions without really executing them]' \
            '*::task:(($tasks))'
            ''
        )
      ;;


        # default completes task names
        (*)
           _command_args='*::task:(($tasks))'
        ;;
    esac

    # -A no options will be completed after the first non-option argument
    _arguments -A : $_command_args
    return 0
}

_doit
