#compdef nikola

_nikola() {
    local -a commands tasks
    # format is 'completion:description'
    commands=(
        'help: show help'
        'check: check links and files in the generated site'
        'forget: clear successful run status from internal DB'
        'console: start an interactive Python console with access to your 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'
        'new_page: create a new page in the site'
        'install_theme: install theme into current site'
        'import_wordpress: import a WordPress dump'
        'plugin: manage plugins'
        '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'
        'clean: clean action / remove targets'
        'github_deploy: deploy the site to GitHub pages'
    )

    # 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.'
'redirect: Generate redirections'
'copy_files: Copy static files into the output folder.'
'generate_rss: Generate RSS feeds.'
'render_pages: Render pages into output.'
'render_site: Group of tasks to render the site.'
'render_sources: Copy page sources into the output.'
'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_listings: Render pretty listings.'
'create_bundles: Bundle assets using WebAssets.'
'post_render: Group of tasks to be executes after site is rendered.'
'robots_file: Generate a robots.txt.'
'_scan_locs: '
'sitemap: Generate a 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))'
            ''
        )
      ;;


      (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.]' \
            '(-v|--verbose)'{-v,--verbose}'[Be more verbose.]' \
            ''
        )
      ;;


      (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))'
            ''
        )
      ;;


      (console)
          _command_args=(
            '(-b|--bpython)'{-b,--bpython}'[Use bpython]' \
            '(-i|--plain)'{-i,--plain}'[Use IPython]' \
            '(-p|--plain)'{-p,--plain}'[Use the plain Python interpreter]' \
            ''
        )
      ;;


      (init)
          _command_args=(
            '(-q|--quiet)'{-q,--quiet}'[Do not ask questions about config.]' \
            '(-d|--demo)'{-d,--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\] ]' \
            '--pdb[get into PDB (python debugger) post-mortem in case of unhadled exception]' \
            '(-s|--single)'{-s,--single}'[Execute only specfied tasks ignoring their task_dep [default: %(default)s\]]' \
            '--strict[Fail on things that would normally be warnings.]' \
            '--invariant[Generate invariant output (for testing only!).]' \
            '(-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. (see also: `nikola new_page`)]' \
            '(-t|--title)'{-t,--title}'[Title for the post.]' \
            '--tags[Comma-separated tags for the post.]' \
            '-1[Create the post with embedded metadata (single file format)]' \
            '-2[Create the post with separate metadata (two file format)]' \
            '-e[Open the post (and meta file, if any) in $EDITOR after creation.]' \
            '(-f|--format)'{-f,--format}'[Markup format for the post, one of rest, markdown, wiki, bbcode, html, textile, txt2tags]' \
            '-s[Schedule the 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\] ]' \
            '--pdb[get into PDB (python debugger) post-mortem in case of unhadled exception]' \
            '(-s|--single)'{-s,--single}'[Execute only specfied tasks ignoring their task_dep [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: 0.0.0.0 – all local interfaces)]' \
            '(-b|--browser)'{-b,--browser}'[Open the test server in a web browser]' \
            ''
        )
      ;;


      (new_page)
          _command_args=(
            '(-t|--title)'{-t,--title}'[Title for the page.]' \
            '-1[Create the page with embedded metadata (single file format)]' \
            '-2[Create the page with separate metadata (two file format)]' \
            '-e[Open the page (and meta file, if any) in $EDITOR after creation.]' \
            '(-f|--format)'{-f,--format}'[Markup format for the page, one of rest, markdown, wiki, bbcode, html, textile, txt2tags]' \
            ''
        )
      ;;


      (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/v7/themes.json)]' \
            ''
        )
      ;;


      (import_wordpress)
          _command_args=(
            '(-o|--output-folder)'{-o,--output-folder}'[Location to write imported content.]' \
            '(-d|--no-drafts)'{-d,--no-drafts}'[Do not 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]' \
            '--qtranslate[Look for translations generated by qtranslate plugin]' \
            '--translations_pattern[The pattern for translation files names]' \
            ''
        )
      ;;


      (plugin)
          _command_args=(
            '(-i|--install)'{-i,--install}'[Install a plugin.]' \
            '(-r|--uninstall)'{-r,--uninstall}'[Uninstall a plugin.]' \
            '(-l|--list)'{-l,--list}'[Show list of available plugins.]' \
            '(-u|--url)'{-u,--url}'[URL for the plugin repository (default: http://plugins.getnikola.com/v7/plugins.json)]' \
            '--user[Install user-wide, available for all sites.]' \
            '--upgrade[Upgrade all installed plugins.]' \
            '--list-installed[List the installed plugins with their location.]' \
            ''
        )
      ;;


      (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))'
            ''
        )
      ;;


      (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))'
            ''
        )
      ;;


      (github_deploy)
          _command_args=(
            
            ''
        )
      ;;


        # 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
