Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

stable version 1.0.0

  • Loading branch information...
commit ec6748a6addb8f4bbbd187b52976759e84099e7d 1 parent 87c5a70
Matthias Guenther authored
Showing with 141 additions and 16 deletions.
  1. +3 −0  .gitignore
  2. +73 −0 doc/tocdown.txt
  3. +41 −2 plugin/tocdown.rb
  4. +24 −14 plugin/tocdown.vim
View
3  .gitignore
@@ -0,0 +1,3 @@
+.DS_Store
+tags
+.tocdown.txt
View
73 doc/tocdown.txt
@@ -0,0 +1,73 @@
+*tocdown.txt* open headings of a markdown document in window
+
+Making Vim more user friendly when dealing with long markdown-files
+
+==================================================================
+CONTENTS *Tocdown-contents*
+ 1. Intro..................|TocdownIntro|
+ 2. Usage..................|TocdownUsage|
+ 3. License................|TocdownLicense|
+ 4. Bugs...................|TocdownBugs|
+ 5. Contributing ..........|TocdownContributing|
+ 6. Changelog .............|TocdownChangelog|
+ 7. Credits ...............|TocdownCredits|
+
+==================================================================
+1. Intro *TocdownIntro*
+
+When you are writing a lon documents and you want to get a fast
+overview about the headlines you have, you want to open a separate
+window to show you briefly the structure of your document.
+
+I got highly inspired by the taglist.vim plugin which provided the
+same functionality just for variables and functions for all kinds
+of different programming languages.
+
+I got stuck what I have done when writing a large book in the
+markdown format.
+
+==================================================================
+2. Usage *TocdownUsage*
+
+When you know what how you want to use this plugin, you can use
+a custom mapping in your |:vimrc| to toggle the toc of a md-file
+
+ nnoremap <F2> :TocdownToggle
+
+You can change the mapping to your needs.
+
+==================================================================
+3. License *TocdownLicense*
+
+MIT.
+
+==================================================================
+4. Bugs *TocdownBugs*
+
+Any bugs by you can be posted on the github page of this
+project: https://github.com/matthias-guenther/tocdown/issues
+
+==================================================================
+5. Contributing *TocdownContributing*
+
+If you have ideas for further features, code improvements, or do
+you know what advanced features of the vimscripting I can use in
+my project? Fork the project on github and send a pull request.
+
+https://github.com/matthias-guenther/tocdown
+
+==================================================================
+6. Changelog *TocdownChangelog*
+
+v1.0.0
+ * initial stable release
+
+==================================================================
+7. Credits *TocdownCredits*
+
+I got highly inspired by Inspired be gundo.vim, nerdtree.vim, and
+taglist.vim. If you want to learn programming vim:
+
+ :help vim-script-intro
+
+is a great way to start the hacking.
View
43 plugin/tocdown.rb
@@ -1,3 +1,42 @@
-path = ARGV[0]
+# find file from ARGV
+file = File.open(ARGV[1], "r")
-File.open(path, "w") { |f| f.puts Time.now}
+headlines = ""
+
+def extract(line)
+ line.gsub!("#", "")
+end
+
+def extract_headline?(text)
+ text.start_with?("#", "##", "###", "####")
+end
+
+def get_indendation(text)
+ # extract the pattern from text
+ # h1 /\*{1} /
+ if text =~ /\#{4}/
+ " "
+ elsif text =~ /\#{3}/
+ " "
+ elsif text =~ /\#{2}/
+ " "
+ elsif text =~ /\#{1}/
+ ""
+ else
+ "too nested"
+ end
+end
+
+# read
+while line = file.gets
+ # go through each line
+ if extract_headline?(line)
+ # get indendation
+ indent = get_indendation(line)
+ # append indentation and extracted lines
+ headlines << indent
+ headlines << extract(line)
+ end
+end
+
+File.open(ARGV[0], "w") { |f| f.puts headlines}
View
38 plugin/tocdown.vim
@@ -1,13 +1,18 @@
" ============================================================================
" File: tocdown.vim
-" Description: cim plugin to display the headlines of a markdown document in
- " an extra window
+" Description: Display the headlines of a markdown document in an extra window
" Maintainer: Matthias Guenther <matthias.guenther@wikimatze.de>
-" License: GPLv2+ -- look it up.
-" Notes: Inspired be gundo.vim and nerdtree.vim
+" License: MIT
+" Notes: Inspired be gundo.vim, nerdtree.vim, and taglist.vim
" ============================================================================
-" global commands
+" check if the plugin is available and avoids loading the plugin twice
+if exists('loaded_tocdown')
+ finish " stops vim from loading the rest of the file
+endif
+let g:loaded_tocdown = 1
+
+" global command to call the function
command TocdownToggle call s:TocdownToggle()
" defining the path of the plugin
@@ -15,19 +20,24 @@ command TocdownToggle call s:TocdownToggle()
" :h suffix drops the last pathname component
let g:plugin_path = escape(expand('<sfile>:p:h'), '\')
+" get the current edited file
+" % says the current file and
+let g:current_file = fnamemodify("%", ":p")
+
function! s:TocdownToggle()
if s:TocdownToggleVisible()
- " quit the buffer
+ " quit the buffer for headlines
exe ':bdelete! _Tocdown_'
else
- " open a new window for the toclist of the markdown headings and go to
- exe ':!ruby ' . g:plugin_path . '/tocdown.rb ' . g:plugin_path . '/.tocdown.txt'
- " the window
- exe ':botright vnew _Tocdown_'
- " read the parsed headlines into the file
- exe ':r ' . g:plugin_path . '/.tocdown.txt'
- " set the buffer to this window
- " tbd.
+ " open a new window for the toclist of the markdown headings
+ " silent press enter when external command demands it
+ exe 'silent :!ruby ' . g:plugin_path . '/tocdown.rb ' . g:plugin_path . '/.tocdown.txt ' . g:current_file
+ " open a new window with the buffername _Tocdown_
+ exe 'botright vnew _Tocdown_'
+ " read the parsed headlines into the the buffer with the _Tocdown_
+ exe 'r ' . g:plugin_path . '/.tocdown.txt'
+ " go the current edited file window
+ exe 1 . "wincmd w"
endif
endfunction
Please sign in to comment.
Something went wrong with that request. Please try again.