diff --git a/.gemspec b/.gemspec deleted file mode 100644 index 72f376e..0000000 --- a/.gemspec +++ /dev/null @@ -1,136 +0,0 @@ -# encoding: utf-8 - -require 'yaml' - -metadata = YAML.load_file('.ruby') - -manifest = Dir.glob('manifest{,.txt}', File::FNM_CASEFOLD).first - - -Gem::Specification.new do |gemspec| - - scm = case - when File.directory?('.git') - :git - end - - files = case - when manifest - File.readlines(manifest). - map{ |line| line.strip }. - reject{ |line| line.empty? || line[0,1] == '#' } - when scm == :git - `git ls-files -z`.split("\0") - else - Dir.glob('{**/}{.*,*}') # TODO: be more specific using standard locations ? - end.select{ |path| File.file?(path) } - - patterns = { - :bin_files => 'bin/*', - :lib_files => 'lib/{**/}*.rb', - :ext_files => 'ext/{**/}extconf.rb', - :doc_files => '*.{txt,rdoc,md,markdown,tt,textile}', - :test_files => '{test/{**/}*_test.rb,spec/{**/}*_spec.rb}' - } - - glob_files = lambda { |pattern| - Dir.glob(pattern).select { |path| - File.file?(path) && files.include?(path) - } - } - - #files = glob_files[patterns[:files]] - - executables = glob_files[patterns[:bin_files]].map do |path| - File.basename(path) - end - - extensions = glob_files[patterns[:ext_files]].map do |path| - File.basename(path) - end - - # build-out the gemspec - - case metadata['revision'] - when 0 - gemspec.name = metadata['name'] - gemspec.version = metadata['version'] - gemspec.summary = metadata['summary'] - gemspec.description = metadata['description'] - - metadata['authors'].each do |author| - gemspec.authors << author['name'] - - if author.has_key?('email') - if gemspec.email - gemspec.email << author['email'] - else - gemspec.email = [author['email']] - end - end - end - - gemspec.licenses = metadata['copyrights'].map{ |c| c['license'] }.compact - - metadata['requirements'].each do |req| - name = req['name'] - version = req['version'] - groups = req['groups'] || [] - - case version - when /^(.*?)\+$/ - version = ">= #{$1}" - when /^(.*?)\-$/ - version = "< #{$1}" - when /^(.*?)\~$/ - version = "~> #{$1}" - end - - if groups.empty? or groups.include?('runtime') - # populate runtime dependencies - if gemspec.respond_to?(:add_runtime_dependency) - gemspec.add_runtime_dependency(name,*version) - else - gemspec.add_dependency(name,*version) - end - else - # populate development dependencies - if gemspec.respond_to?(:add_development_dependency) - gemspec.add_development_dependency(name,*version) - else - gemspec.add_dependency(name,*version) - end - end - end - - # convert external dependencies into a requirements - if metadata['external_dependencies'] - ##gemspec.requirements = [] unless metadata['external_dependencies'].empty? - metadata['external_dependencies'].each do |req| - gemspec.requirements << req.to_s - end - end - - # determine homepage from resources - homepage = metadata['resources'].find{ |key, url| key =~ /^home/ } - gemspec.homepage = homepage.last if homepage - - gemspec.require_paths = metadata['load_path'] || ['lib'] - gemspec.post_install_message = metadata['install_message'] - - # RubyGems specific metadata - gemspec.files = files - gemspec.extensions = extensions - gemspec.executables = executables - - if Gem::VERSION < '1.7.' - gemspec.default_executable = gemspec.executables.first - end - - gemspec.test_files = glob_files[patterns[:test_files]] - - unless gemspec.files.include?('.document') - gemspec.extra_rdoc_files = glob_files[patterns[:doc_files]] - end - end -end diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..8d5fbaa --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,30 @@ +name: CI + +on: + push: + pull_request: + workflow_dispatch: + # Monthly, to catch breakage from new Ruby or dependency releases + # when nothing has been pushed. + schedule: + - cron: '0 6 1 * *' + +permissions: + contents: read + +jobs: + test: + name: Ruby ${{ matrix.ruby }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + ruby: ['3.1', '3.2', '3.3', '3.4', '4.0'] + steps: + - uses: actions/checkout@v4 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - name: Tests + run: bundle exec rake test diff --git a/.gitignore b/.gitignore index 5fe6d03..c19a99c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,9 @@ -.reap/digest +.rdoc .yardoc -doc log pkg -ri tmp -site/doc -web \ No newline at end of file +doc +web +*.gem +Gemfile.lock diff --git a/.reap/ignore b/.reap/ignore deleted file mode 100644 index 0b0eadf..0000000 --- a/.reap/ignore +++ /dev/null @@ -1,2 +0,0 @@ -ri -work diff --git a/.reap/pack.reap b/.reap/pack.reap deleted file mode 100644 index c1a379d..0000000 --- a/.reap/pack.reap +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env ruby - -trip :pack do - system "gem build .gemspec" - system "mv *.gem pkg/" -end - diff --git a/.reap/spec.reap b/.reap/spec.reap deleted file mode 100644 index 251daac..0000000 --- a/.reap/spec.reap +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env ruby - -file "Profile", "Version" do - system "pom spec -u" - system "pom gemspec -u" -end - -#file "Version" do -# system "pom spec -u" -# system "pom gemspec -u" -#end - -file "Manifest", "MANIFEST" do - system "pom gemspec -u" -end - diff --git a/.reap/test.reap b/.reap/test.reap deleted file mode 100644 index 2792a37..0000000 --- a/.reap/test.reap +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env ruby - -file "lib/**/*.rb" do - system "lemon -Ilib test/*_case.rb" -end - -trip :test do - system "lemon -Ilib test/*_case.rb" -end - diff --git a/.ruby b/.ruby deleted file mode 100644 index 1407272..0000000 --- a/.ruby +++ /dev/null @@ -1,55 +0,0 @@ ---- -source: -- var -authors: -- name: Thomas Sawyer - email: transfire@gmail.com -copyrights: -- holder: Thomas Sawyer, Rubyworks - year: '2009' -replacements: [] -alternatives: [] -requirements: -- name: lemon - groups: - - test - development: true -- name: ae - groups: - - test - development: true -- name: detroit - groups: - - build - development: true -- name: reap - groups: - - build - development: true -dependencies: [] -conflicts: [] -repositories: -- uri: git://github.com/rubyworks/dnote.git - scm: git - name: upstream -resources: - home: http://rubyworks.github.com/dnote - code: http://github.com/rubyworks/dnote - wiki: http://wiki.github.com/rubyworks/dnote - api: http://rubydoc.info/gems/dnote - bugs: http://github.com/rubyworks/dnote/issues - mail: http://groups.google.com/groups/rubyworks-mailinglist -extra: {} -load_path: -- lib -revision: 0 -created: '2009-10-09' -summary: Extract developer's notes from source code. -title: DNote -version: 1.7.0 -name: dnote -description: ! 'DNote makes it easy to extract developer''s notes from source code, - - and supports almost any language.' -organization: RubyWorks -date: '2011-11-11' diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 012d328..0000000 --- a/.travis.yml +++ /dev/null @@ -1,10 +0,0 @@ ---- -rvm: - - 1.8.7 - - 1.9.2 - - 1.9.3 - - rbx-2.0 - - jruby - - ree -script: "bundle exec ruby-test test/*.rb" - diff --git a/Assembly b/Assembly deleted file mode 100644 index 2be838d..0000000 --- a/Assembly +++ /dev/null @@ -1,35 +0,0 @@ ---- -email: - service : Email - mailto : - - ruby-talk@ruby-lang.org - - rubyworks-mailinglist@googlegroups.com - -gem: - active: true - -github: - gh_pages: web - -yard: - priority: 2 - -dnote: - labels : ~ - lines : 5 - output : - - log/NOTES.rdoc - -vclog: - output: - - log/Changes.rdoc - - log/History.rdoc - -#lemon: -# tests : ~ -# exclude : ~ -# loadpath : ~ -# requires : ~ -# live : false -# active : false - diff --git a/Gemfile b/Gemfile index e45e65f..3be9c3c 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,2 @@ -source :rubygems +source "https://rubygems.org" gemspec diff --git a/HISTORY.rdoc b/HISTORY.rdoc index 203ce65..8de9b4f 100644 --- a/HISTORY.rdoc +++ b/HISTORY.rdoc @@ -1,6 +1,45 @@ = RELEASE HISTORY +== 1.8.0 / UNRELEASED + +DNote runs on current Ruby again; it needs Ruby 3.1 or newer. Notes can +now be written as tags, in the manner of YARD, e.g. `@TODO Fix this.` +A tag in capitals is always a note. The usual labels (TODO, FIXME, +OPTIMIZE, THINK and DEPRECATE) are notes in lower case too, so YARD's +own `@todo` is collected. Use `--no-tags` to turn tags off. + +Changes: + +* Support Ruby 3.1 and newer (ERB is called with keywords). +* Add tag notes, such as `@TODO`, and the `--[no-]tags` option. +* A tag, such as YARD's `@return`, ends the note before it. +* Fix the first line of code after a note being left out of its context. +* Fix `html/list` and `xml/file` formats, which failed with any notes. +* Fix `xoxo` formats, which never finished. +* Fix label-and-file groupings when the plain grouping was used first. +* Fix the Rake task, which was written for another tool's task library. +* Fix `--dryrun`, which wrote the file all the same. +* Escape code context in HTML formats. +* Read files that aren't valid UTF-8, rather than fail on them. +* The string methods the templates use are refinements, no longer + added to String itself. +* A format whose library isn't installed (soap, xoxo) says so. +* Recognize the comment markers of more languages. +* Drop the dependency on REXML. + + +== 1.7.1 / 2012-01-10 + +The `dnote.yml` file which provides project metadata, particularly the DNote::VERSION setting, +was left out of the package manifest. This release fixes this issue and gets things working +again. + +Changes: + +* Add lib/dnote.yml to MANIFEST. + + == 1.7.0 / 2011-10-25 This release finishes adding code context support to all formats. Use the `-c` diff --git a/MANIFEST b/MANIFEST deleted file mode 100644 index 1720ec2..0000000 --- a/MANIFEST +++ /dev/null @@ -1,55 +0,0 @@ -#!mast .ruby .yardopts bin lib man spec test try [A-Z]*.* -.ruby -.yardopts -bin/dnote -lib/dnote/core_ext.rb -lib/dnote/format.rb -lib/dnote/note.rb -lib/dnote/notes.rb -lib/dnote/rake/dnotetask.rb -lib/dnote/session.rb -lib/dnote/templates/html/file.erb -lib/dnote/templates/html/label.erb -lib/dnote/templates/html/list.erb -lib/dnote/templates/html.erb -lib/dnote/templates/json/file.erb -lib/dnote/templates/json/label.erb -lib/dnote/templates/json/list.erb -lib/dnote/templates/json.erb -lib/dnote/templates/md/file.erb -lib/dnote/templates/md/label.erb -lib/dnote/templates/md/list.erb -lib/dnote/templates/md.erb -lib/dnote/templates/rdoc/file.erb -lib/dnote/templates/rdoc/label.erb -lib/dnote/templates/rdoc/list.erb -lib/dnote/templates/rdoc.erb -lib/dnote/templates/soap/file.erb -lib/dnote/templates/soap/label.erb -lib/dnote/templates/soap/list.erb -lib/dnote/templates/soap.erb -lib/dnote/templates/text/file.erb -lib/dnote/templates/text/label.erb -lib/dnote/templates/text/list.erb -lib/dnote/templates/text.erb -lib/dnote/templates/xml/file.erb -lib/dnote/templates/xml/label.erb -lib/dnote/templates/xml/list.erb -lib/dnote/templates/xml.erb -lib/dnote/templates/xoxo/file.erb -lib/dnote/templates/xoxo/label.erb -lib/dnote/templates/xoxo/list.erb -lib/dnote/templates/xoxo.erb -lib/dnote/templates/yaml/file.erb -lib/dnote/templates/yaml/label.erb -lib/dnote/templates/yaml/list.erb -lib/dnote/templates/yaml.erb -lib/dnote/version.rb -lib/dnote.rb -test/notes_case.rb -try/sample.bas -try/sample.js -try/sample.rb -HISTORY.rdoc -README.rdoc -COPYING.rdoc diff --git a/README.rdoc b/README.rdoc index d2103db..ab81751 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,8 +1,9 @@ = DNote -* {Homepage}[http://rubyworks.github.com/dnote] -* {Mailing List}[http://googlegroups.com/group/rubyworks-mailinglist] -* {Source Code}[http://github.com/rubyworks/dnote] +{Source Code}[https://github.com/rubyworks/dnote] / +{Report Issue}[https://github.com/rubyworks/dnote/issues] / +{API}[https://rubydoc.info/gems/dnote] +{CI}[https://github.com/rubyworks/dnote/actions/workflows/ci.yml] == DESCRIPTION @@ -30,7 +31,7 @@ the note for a pre block if it is indented. # LABEL: description ... # continue ... -3) An alternative to the previous limitation is to indent the whole note, making it +3) An alternative to the previous limitation is to indent the whole note, making it a <pre> block when rendered by RDoc. Then the text layout is free-form. # This is a description of something... @@ -38,13 +39,33 @@ a <pre> block when rendered by RDoc. Then the text layout is free # LABEL: description ... # continue ... -That's all there is to it, if I can convince the developers of RDoc to recognize labels, -we may eventually be able to relax the flush rule too, which would be very nice. +A note ends at a blank comment line or at a line of code. There is also a command-line option, --no-colon, which deactives the need for a colon after the note label. However this often produces false positives, so it's use is discouraged. +=== Tags + +A note can also be written as a tag, in the manner of YARD. A tag needs no colon. + + # @LABEL description ... + # continue ... + +A tag in capitals is always taken as a note. The usual labels, +TODO+, +FIXME+, ++OPTIMIZE+, +THINK+ and +DEPRECATE+, are taken as notes in lower case too, so YARD's +own @todo tag is collected along with the rest. Other lower case tags, such +as @param and @return, are documentation and are left alone, +unless asked for by name, e.g. --label NOTE collects @note. + +A tag note ends at the next tag, as well as at a blank comment line or a line of code. + +YARD itself warns of a tag it doesn't know, such as @FIXME, and leaves it out +of the documentation. To have YARD accept such a tag, name it in +.yardopts+, +e.g. --tag FIXME:"Fix Me", and add --hide-tag FIXME +to keep it out of the documentation. + +To turn tags off use --no-tags. === Generating Notes @@ -53,55 +74,90 @@ As you can see the commandline interface is pretty straight-forward. USAGE: dnote [OPTIONS] path1 [path2 ...] - + OUTPUT FORMAT: (choose one) -f, --format NAME select a format [text] - -c, --custom FILE use a custom ERB template + -C, --custom FILE use a custom ERB template --file shortcut for text/file format --list shortcut for text/list format - + OTHER OPTIONS: -l, --label LABEL labels to collect --[no-]colon match labels with/without colon suffix + --[no-]tags match labels written as tags, e.g. @TODO [yes] -m, --marker MARK alternative remark marker - -u --url TEMPLATE url template for line entries (for HTML) + -u, --url TEMPLATE url template for line entries (for HTML) + -c, --context INTEGER number of lines of context to display -x, --exclude PATH exclude file or directory - -i, --ignore NAME ignore based on any part of the pathname + -i, --ignore NAME ignore file based on any part of pathname -t, --title TITLE title to use in header - -o, --output PATH name of file or directory + -o, --output PATH save to file or directory -n, --dryrun do not actually write to disk --debug debug mode - + COMMAND OPTIONS: -T, --templates list available format templates -h, --help show this help information -The default path is **/*.rb and the default format is -f rdoc. -Here is an example of DNote's current notes in RDoc format: +The default path is **/*.rb and the default format is -f text. +A path that is a directory takes in everything below it. The comment marker is +chosen by a file's extension (# where it isn't known); use --marker +for any other. + +Here is an example of DNote's notes in RDoc format, from +dnote -f rdoc -l TODO lib/dnote/format.rb: - = Development Notes + = Developer's Notes == TODO - === file://lib/dnote/notes.rb + === file://lib/dnote/format.rb - * TODO: Add ability to read header notes. They often - have a outline format, rather then the single line. (19) - * TODO: Need good CSS file. (22) - * TODO: Need XSL? (24) + * TODO: Need good CSS file. (12) + * TODO: Need XSL? (14) - === file://plug/syckle/services/dnote.rb + (2 TODOs) - * TODO: Should this service be part of the +site+ cycle? (18) +=== Formats - (4 TODOs) +The formats are +text+, +md+, +rdoc+, +html+, +xml+, +json+, +yaml+, +soap+ +and +xoxo+. Each arranges the notes by label and then file. Add /file, +/label or /list for another arrangement, e.g. -f md/list. + +The +soap+ format needs the +soap4r-ng+ gem and the +xoxo+ format needs the +xoxo+ gem. +Neither is installed with DNote. + +A custom template is an ERB file. It has +notes+ and +title+ to work with, and ++h+ to escape text for HTML or XML. + +=== Rake Task + + require 'dnote/rake/dnotetask' + + DNote::RakeTask.new do |t| + t.files = 'lib/**/*.rb' + t.formats = ['html', 'md'] + t.output = 'doc/notes' + end + +This defines a +dnote+ task, which writes doc/notes/notes.html and +doc/notes/notes.md, and a dnote:clobber task that removes them. + +=== With YARD + +To add the notes to YARD documentation, write them to a file and list it in ++.yardopts+ with the other extra files. + + $ dnote -f md -o NOTES.md == INSTALLATION The usual RubyGem's command will do the trick. - $ sudo gem install dnote + $ gem install dnote + +DNote needs Ruby 3.1 or newer. == COPYRIGHT diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..fac6810 --- /dev/null +++ b/Rakefile @@ -0,0 +1,21 @@ +require 'rake/clean' +CLOBBER.include 'pkg' + +require 'bundler/gem_helper' + +# Bundler tags releases as v1.2.3, but DNote's tags have always been +# plain version numbers, so keep it that way. +class Bundler::GemHelper + def version_tag + version.to_s + end +end + +Bundler::GemHelper.install_tasks + +desc 'run tests' +task 'test' do + sh 'rubytest -Ilib -r lemon test/*_case.rb' +end + +task 'default' => 'test' diff --git a/dnote.gemspec b/dnote.gemspec new file mode 100644 index 0000000..aeec115 --- /dev/null +++ b/dnote.gemspec @@ -0,0 +1,33 @@ +require_relative 'lib/dnote/version' + +Gem::Specification.new do |s| + s.name = 'dnote' + s.version = DNote::VERSION + s.summary = "Extract developer's notes from source code" + s.description = "DNote makes it easy to extract developer's notes from source " \ + "code, and supports almost any language." + + s.authors = ['Trans'] + s.email = ['transfire@gmail.com'] + s.homepage = 'https://github.com/rubyworks/dnote' + s.license = 'BSD-2-Clause' + + s.metadata = { + 'source_code_uri' => 'https://github.com/rubyworks/dnote', + 'bug_tracker_uri' => 'https://github.com/rubyworks/dnote/issues', + 'changelog_uri' => 'https://github.com/rubyworks/dnote/blob/master/HISTORY.rdoc', + 'documentation_uri' => 'https://rubydoc.info/gems/dnote' + } + + s.required_ruby_version = '>= 3.1' + + s.files = Dir['lib/**/*.rb', 'lib/**/*.erb', 'bin/*', 'README.rdoc', 'HISTORY.rdoc', 'COPYING.rdoc'] + s.bindir = 'bin' + s.executables = ['dnote'] + s.require_paths = ['lib'] + + s.add_development_dependency 'rake', '>= 13' + s.add_development_dependency 'rubytest', '>= 0.11.1' + s.add_development_dependency 'lemon', '>= 0.9.4' + s.add_development_dependency 'ae', '>= 1.9' +end diff --git a/lib/dnote.yml b/lib/dnote.yml deleted file mode 120000 index 064ee51..0000000 --- a/lib/dnote.yml +++ /dev/null @@ -1 +0,0 @@ -../.ruby \ No newline at end of file diff --git a/lib/dnote/core_ext.rb b/lib/dnote/core_ext.rb index d66abad..d043646 100644 --- a/lib/dnote/core_ext.rb +++ b/lib/dnote/core_ext.rb @@ -1,92 +1,71 @@ -module Enumerable - - # Taken from Ruby Facets. - def group_by #:yield: - #h = k = e = nil - r = Hash.new - each{ |e| (r[yield(e)] ||= []) << e } - r - end unless method_defined?(:group_by) - -end - module DNote - # Extensions for String class. - # These methods are taken directly from Ruby Facets. + # String methods used by the templates. They are refinements, so they + # are not added to String for the rest of the program. A custom template + # has them, since it is rendered where they are in effect. # - module String + # These methods are taken from Ruby Facets. + module StringMethods - # Provides a margin controlled string. - # - # x = %Q{ - # | This - # | is - # | margin controlled! - # }.margin - # - # - # NOTE: This may still need a bit of tweaking. - # - # CREDIT: Trans + refine ::String do - def margin(n=0) - #d = /\A.*\n\s*(.)/.match( self )[1] - #d = /\A\s*(.)/.match( self)[1] unless d - d = ((/\A.*\n\s*(.)/.match(self)) || - (/\A\s*(.)/.match(self)))[1] - return '' unless d - if n == 0 - gsub(/\n\s*\Z/,'').gsub(/^\s*[#{d}]/, '') - else - gsub(/\n\s*\Z/,'').gsub(/^\s*[#{d}]/, ' ' * n) + # Provides a margin controlled string. + # + # x = %Q{ + # | This + # | is + # | margin controlled! + # }.margin + # + # CREDIT: Trans + def margin(n=0) + d = ((/\A.*\n\s*(.)/.match(self)) || + (/\A\s*(.)/.match(self)))[1] + return '' unless d + if n == 0 + gsub(/\n\s*\Z/,'').gsub(/^\s*[#{d}]/, '') + else + gsub(/\n\s*\Z/,'').gsub(/^\s*[#{d}]/, ' ' * n) + end end - end - - # Preserves relative tabbing. - # The first non-empty line ends up with n spaces before nonspace. - # - # CREDIT: Gavin Sinclair - def tabto(n) - if self =~ /^( *)\S/ - indent(n - $1.length) - else - self + # Preserves relative tabbing. + # The first non-empty line ends up with n spaces before nonspace. + # + # CREDIT: Gavin Sinclair + def tabto(n) + if self =~ /^( *)\S/ + indent(n - $1.length) + else + self + end end - end - # Indent left or right by n spaces. - # (This used to be called #tab and aliased as #indent.) - # - # CREDIT: Gavin Sinclair - # CREDIT: Trans - - def indent(n) - if n >= 0 - gsub(/^/, ' ' * n) - else - gsub(/^ {0,#{-n}}/, "") + # Indent left or right by n spaces. + # + # CREDIT: Gavin Sinclair + # CREDIT: Trans + def indent(n) + if n >= 0 + gsub(/^/, ' ' * n) + else + gsub(/^ {0,#{-n}}/, "") + end end - end - # - # - def tabset(n) - i = lines.map do |line| - line.strip.empty? ? nil : line.index(/\S/) + # Indent so that the least indented line starts at column n. + def tabset(n) + i = lines.map do |line| + line.strip.empty? ? nil : line.index(/\S/) + end + x = i.compact.min + t = n - x.to_i + t = 0 if t < 0 + indent(t) end - x = i.compact.min - t = n - x.to_i - t = 0 if t < 0 - indent(t) - end - end + end - class ::String #:nodoc: - include DNote::String end end - diff --git a/lib/dnote/format.rb b/lib/dnote/format.rb index 5f507c5..95f0220 100644 --- a/lib/dnote/format.rb +++ b/lib/dnote/format.rb @@ -1,5 +1,11 @@ +require 'dnote/core_ext' + module DNote + # The string methods the templates use (+indent+, +tabset+ and so on) + # are refinements, in effect here and so in any template rendered. + using DNote::StringMethods + # = Notes Formatter # #-- @@ -11,8 +17,6 @@ class Format require 'fileutils' require 'erb' - require 'rexml/text' - require 'dnote/core_ext' #DEFAULT_OUTPUT_DIR = "log/dnote" @@ -79,8 +83,12 @@ def render_custom def render_template template = File.join(File.dirname(__FILE__), 'templates', "#{format}.erb") raise "No such format - #{format}" unless File.exist?(template) - result = erb(template) - publish(result) + begin + result = erb(template) + rescue LoadError => err + raise "The #{format} format needs a library that is not installed (#{err.message})." + end + publish(result) end private @@ -115,7 +123,7 @@ def write(result, fname=nil) puts "write: #{file}" else dir = File.dirname(file) - fu.mkdir(dir) unless File.exist?(dir) + fu.mkdir_p(dir) unless File.exist?(dir) File.open(file, 'w'){ |f| f << result } end return file @@ -154,12 +162,12 @@ def initialize(data={}) end # def render(file) - erb = ERB.new(File.read(file), nil, '<>') + erb = ERB.new(File.read(file), trim_mode: '<>') erb.result(binding) end # def h(string) - REXML::Text.normalize(string) + ERB::Util.html_escape(string) end # def method_missing(s, *a) diff --git a/lib/dnote/notes.rb b/lib/dnote/notes.rb index 9a29977..c2156d5 100644 --- a/lib/dnote/notes.rb +++ b/lib/dnote/notes.rb @@ -7,10 +7,14 @@ module DNote # # This class goes through you source files and compiles a list # of any labeled comments. Labels are all-cap single word prefixes - # to a comment ending in a colon. + # to a comment ending in a colon, as in TODO: Do something. # - # Special labels do not require the colon. By default these are - # +TODO+, +FIXME+, +OPTIMIZE+, +THINK+ and +DEPRECATE+. + # A label can also be written as a tag, in the manner of YARD, in which + # case the colon isn't needed, as in @TODO Do something. + # + # The usual labels, +TODO+, +FIXME+, +OPTIMIZE+, +THINK+ and +DEPRECATE+, + # are recognized as tags in lower case too, so YARD's own @todo + # tag is collected. # #-- # TODO: Add ability to read header notes. They often @@ -22,7 +26,8 @@ class Notes # Default paths (all ruby scripts). DEFAULT_PATHS = ["**/*.rb"] - # Default note labels to look for in source code. (NOT CURRENTLY USED!) + # The usual note labels. When no labels are asked for, all labels are + # collected, and these are also recognized as lower-case tags. DEFAULT_LABELS = ['TODO', 'FIXME', 'OPTIMIZE', 'THINK', 'DEPRECATE'] # Files to search for notes. @@ -37,6 +42,9 @@ class Notes # Specific remark marker (+nil+ for auto). attr_accessor :marker + # Recognize tag notes, such as @TODO? Default is +true+. + attr_accessor :tags + # Link template. attr_accessor :url @@ -49,6 +57,7 @@ def initialize(files, options={}) @labels = [options[:labels] || DEFAULT_LABELS].flatten.compact @colon = options[:colon].nil? ? true : options[:colon] @marker = options[:marker] #|| '#' + @tags = options[:tags].nil? ? true : options[:tags] @url = options[:url] @context = options[:context] || 0 @@ -95,6 +104,7 @@ def parse mark = remark(fname) lineno, note, text, capt = 0, nil, nil, nil File.readlines(fname).each do |line| + line = line.scrub unless line.valid_encoding? #while line = f.gets lineno += 1 note = match(line, lineno, fname) @@ -110,15 +120,21 @@ def parse when /^\s*#{mark}+\s*$/, /^\s*#{mark}\-\-/, /^\s*#{mark}\+\+/ text.strip! text = nil + when /^\s*#{mark}\s*@\w/ + # another tag, such as YARD's `@return`, ends the note + text.strip! + text = nil when /^\s*#{mark}/ - if text[-1,1] == "\n" + if text.empty? || text[-1,1] == "\n" text << line.gsub(/^\s*#{mark}\s*/,'') else text << "\n" << line.gsub(/^\s*#{mark}\s*/,'') end else + # a line of code ends the note, and is its first line of context text.strip! text = nil + capt << line if capt && capt.size < context end else if line !~ /^\s*#{mark}/ @@ -135,11 +151,43 @@ def parse # Is this line a note? def match(line, lineno, file) + note = ( + if labels.empty? + match_general(line, lineno, file) + else + match_special(line, lineno, file) + end + ) + note || (tags ? match_tag(line, lineno, file) : nil) + end + + # Match notes written as tags, such as @TODO. A tag has no + # need of a colon, though it may have one. + # + # A tag in capitals is always a note. In lower case it is a note only + # if it is one of the labels asked for, or one of the usual labels + # when none were asked for. So YARD's @todo is collected, and + # its @param and @return are not. + def match_tag(line, lineno, file) + md = match_tag_regex(file).match(line) + return nil unless md + + name, text = md[1], md[2].to_s + label = name.upcase + if labels.empty? - match_general(line, lineno, file) + return nil unless name == label || DEFAULT_LABELS.include?(label) else - match_special(line, lineno, file) + return nil unless labels.include?(label) end + + Note.new(self, file, label, lineno, text, remark(file)) + end + + # + def match_tag_regex(file) + mark = remark(file) + /#{mark}\s*@([A-Za-z]+)[:]?(?:\s+(.*?))?$/ end # Match special notes. @@ -155,9 +203,7 @@ def match_special(line, lineno, file) rec end - #-- - # TODO: ruby-1.9.1-p378 reports: `match': invalid byte sequence in UTF-8 - #++ + # def match_special_regex(label, file) mark = remark(file) if colon @@ -218,7 +264,7 @@ def by_file # Organize notes into a hash with labels for keys, followed # by a hash with filename for keys. def by_label_file - @by_label ||= ( + @by_label_file ||= ( list = {} notes.each do |note| list[note.label] ||= {} @@ -233,7 +279,7 @@ def by_label_file # Organize notes into a hash with filenames for keys, followed # by a hash with labels for keys. def by_file_label - @by_file ||= ( + @by_file_label ||= ( list = {} notes.each do |note| list[note.file] ||= {} @@ -270,11 +316,11 @@ def remark(file) def guess_marker(file) return @marker if @marker # forced marker case File.extname(file) - when '.js', '.c', 'cpp', '.css' + when '.js', '.c', '.cpp', '.h', '.css', '.java', '.go', '.rs', '.ts' '//' when '.bas' "'" - when '.sql', '.ada' + when '.sql', '.ada', '.lua', '.hs' '--' when '.asm' ';' diff --git a/lib/dnote/rake/dnotetask.rb b/lib/dnote/rake/dnotetask.rb index f603471..9130699 100644 --- a/lib/dnote/rake/dnotetask.rb +++ b/lib/dnote/rake/dnotetask.rb @@ -1,21 +1,37 @@ +require 'rake' +require 'rake/tasklib' +require 'rake/clean' + module DNote - # = Developmer's Notes Rake Task + # = Developer's Notes Rake Task + # + # require 'dnote/rake/dnotetask' + # + # DNote::RakeTask.new do |t| + # t.files = 'lib/**/*.rb' + # t.formats = ['html', 'md'] + # t.output = 'doc/notes' + # end # + # This defines a +dnote+ task that writes the notes, and a + # +dnote:clobber+ task that removes them, which +clobber+ also runs. class RakeTask < Rake::TaskLib - require 'rake/clean' - # Default note labels to looked for in source code. DEFAULT_LABELS = ['TODO', 'FIXME', 'OPTIMIZE', 'DEPRECATE'] + # Name of the task. Defaults to +dnote+. + attr_accessor :name + # File paths to search. attr_accessor :files - # Labels to document. Defaults are: TODO, FIXME, OPTIMIZE and DEPRECATE. + # Labels to document. By default all labels are collected. attr_accessor :labels - # Formats (xml, html, rdoc, rdoc/list and so on). + # Formats (xml, html, rdoc, rdoc/list and so on). The default is + # +index+, which is the +html+ format saved as index.html. attr_accessor :formats # Exclude paths. @@ -24,11 +40,11 @@ class RakeTask < Rake::TaskLib # Ignore paths based on any part of pathname. attr_accessor :ignore - # Output directory to save notes file. Defaults to dnote/ under - # the project log directory (eg. log/dnote/). - attr_accessor :output + # Output directory to save notes file. Defaults to log/dnote/. + # It is made if it does not exist. + attr_reader :output - # Title to use if temaplte can use it. + # Title to use if template can use it. attr_accessor :title # @@ -36,79 +52,82 @@ def output=(path) @output = Pathname.new(path) end + # Define the tasks. The task object is yielded for settings. + def initialize(name='dnote') + @name = name.to_s + init + yield(self) if block_given? + define + end + # def init + require 'pathname' require 'dnote' require 'dnote/format' @files = "**/*.rb" - @output = 'log/dnote' + @output = Pathname.new('log/dnote') @formats = ['index'] - @labels = nil #DEFAULT_LABELS + @labels = nil end # def define desc "Collect Developer's Notes" - task 'dnote' do + task name do document end - task 'dnote:clobber' do + task "#{name}:clobber" do clean end - task :clobber => ['dnote:clobber'] + task :clobber => ["#{name}:clobber"] end # Generate notes document(s). def document - abort "dnote: #{output} is not a directory" unless output.directory? - - session = ::DNote::Session.new do |s| - s.paths = files - s.exclude = exclude - s.ignore = ignore - s.labels = labels #|| DEFAULT_LABELS - s.title = title - s.output = output - s.dryrun = application.options.dryrun #trial? - end + abort "dnote: #{output} is not a directory" if output.exist? && !output.directory? + + dryrun = Rake.application.options.dryrun formats.each do |format| + session = ::DNote::Session.new do |s| + s.paths = [files].flatten.compact + s.exclude = exclude + s.ignore = ignore + s.labels = [labels].flatten.compact + s.title = title if title + s.dryrun = dryrun + end + if format == 'index' session.format = 'html' - session.output = File.join(self.output, 'index.html') + session.output = (output + 'index.html').to_s else session.format = format + session.output = output.to_s + '/' end + + output.mkpath unless dryrun session.run - report "Updated #{output.to_s.sub(Dir.pwd+'/','')}" unless trial? end end - # Reset output directory, marking it as out-of-date. - def reset - #if File.directory?(output) - File.utime(0,0,output) unless $NOOP - puts "Marked #{output} as out-of-date" - #end - end - # Remove output files. def clean - #if File.directory?(output) - formats.each do |format| - if format == 'index' - file = (output + "index.html").to_s - else - ext = ::DNote::Format::EXTENSIONS[format] || format - file = (output + "notes.#{ext}").to_s - end - rm(file) - report "Removed #{output}" - end - #else - # rm(output) - # report "Removed #{output}" - #end + formats.each do |format| + rm_f output_file(format).to_s + end + end + + # The file a format is written to. + def output_file(format) + if format == 'index' + output + "index.html" + else + type = format.split('/').first + ext = ::DNote::Format::EXTENSIONS[type] || type + output + "notes.#{ext}" + end end end diff --git a/lib/dnote/session.rb b/lib/dnote/session.rb index a0c911b..17950c3 100644 --- a/lib/dnote/session.rb +++ b/lib/dnote/session.rb @@ -1,6 +1,5 @@ module DNote - require 'dnote/core_ext' require 'dnote/notes' require 'dnote/format' @@ -41,6 +40,10 @@ class Session # Alternate remark marker. Useful to other languages besides Ruby. attr_accessor :marker + # Collect notes written as tags, such as @TODO. The default + # is true. + attr_accessor :tags + # Output format. attr_accessor :format @@ -87,6 +90,7 @@ def initialize_defaults @title = DEFAULT_TITLE @dryrun = false @marker = nil + @tags = true @url = nil @context = 0 end @@ -105,12 +109,13 @@ def ignore=(list) # Run session. def run - notes = Notes.new(files, :labels=>labels, :colon=>colon, :marker=>marker, :url=>url, :context=>context) + notes = Notes.new(files, :labels=>labels, :colon=>colon, :marker=>marker, :tags=>tags, :url=>url, :context=>context) formatter = Format.new(notes) do |f| f.format = format f.template = template f.title = title f.output = output + f.dryrun = dryrun end formatter.render end @@ -195,6 +200,10 @@ def self.main(*argv) session.colon = val end + opt.on("--[no-]tags", "match labels written as tags, e.g. @TODO [yes]") do |val| + session.tags = val + end + opt.on("--marker", "-m MARK", "alternative remark marker") do |mark| session.marker = mark end diff --git a/lib/dnote/templates/html.erb b/lib/dnote/templates/html.erb index a16d15e..4091ce6 100644 --- a/lib/dnote/templates/html.erb +++ b/lib/dnote/templates/html.erb @@ -34,7 +34,7 @@ <%= h note.text %> <%= note.line %> <% if note.code? %>
-<%= note.code %>
+<%= h note.code %>
 
<% end %> diff --git a/lib/dnote/templates/html/file.erb b/lib/dnote/templates/html/file.erb index 9d9beda..faa9c9c 100644 --- a/lib/dnote/templates/html/file.erb +++ b/lib/dnote/templates/html/file.erb @@ -33,7 +33,7 @@ <%= h note.textline %> <%= note.line %> <% if note.code? %>
-<%= note.code %>
+<%= h note.code %>
 
<% end %> diff --git a/lib/dnote/templates/html/label.erb b/lib/dnote/templates/html/label.erb index 3de45ba..cb1e019 100644 --- a/lib/dnote/templates/html/label.erb +++ b/lib/dnote/templates/html/label.erb @@ -34,7 +34,7 @@ <%= h note.text %> <%= note.line %> <% if note.code? %>
-<%= note.code %>
+<%= h note.code %>
 
<% end %> diff --git a/lib/dnote/templates/html/list.erb b/lib/dnote/templates/html/list.erb index fd0c505..9df6395 100644 --- a/lib/dnote/templates/html/list.erb +++ b/lib/dnote/templates/html/list.erb @@ -30,9 +30,9 @@ <%= note.file %>: <%= note.line %> <% if note.code? %>
-<%= note.code %>
+<%= h note.code %>
 
-<$ end %> +<% end %> <% end %> diff --git a/lib/dnote/templates/xml/file.erb b/lib/dnote/templates/xml/file.erb index 0892ba0..fb173a1 100644 --- a/lib/dnote/templates/xml/file.erb +++ b/lib/dnote/templates/xml/file.erb @@ -2,7 +2,7 @@ <% notes.by_file_label.each do |file, per_label| %> -<% per_label.each do |label, lnotes| %> +<% per_label.each do |label, line_notes| %>