diff --git a/lib/jsduck/format/class.rb b/lib/jsduck/format/class.rb index 7ba40c61103624bde9703f55f88cafb0f6d92f7a..b2ea6b52d07b628a6f2d839f4306e4744408c6d1 100644 --- a/lib/jsduck/format/class.rb +++ b/lib/jsduck/format/class.rb @@ -1,7 +1,7 @@ require 'jsduck/type_parser' require 'jsduck/logger' require 'jsduck/tag_registry' -require 'jsduck/shortener' +require 'jsduck/format/shortener' require 'jsduck/util/html' module JsDuck @@ -41,8 +41,8 @@ module JsDuck def format_member(m) @formatter.doc_context = m[:files][0] format_tags(m) - if expandable?(m) || Shortener.too_long?(m[:doc]) - m[:shortDoc] = Shortener.shorten(m[:doc]) + if expandable?(m) || Format::Shortener.too_long?(m[:doc]) + m[:shortDoc] = Format::Shortener.shorten(m[:doc]) end # We don't validate and format CSS var and mixin type definitions diff --git a/lib/jsduck/format/shortener.rb b/lib/jsduck/format/shortener.rb new file mode 100644 index 0000000000000000000000000000000000000000..35427710fdd2693758281c56f7d12586d4790f63 --- /dev/null +++ b/lib/jsduck/format/shortener.rb @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +require 'jsduck/util/html' +require 'jsduck/util/singleton' + +module JsDuck + module Format + + # Little helper for shortening text + class Shortener + include Util::Singleton + + # Maximum length for text that doesn't get shortened. + # The accessor is used for testing purposes only. + attr_accessor :max_length + + def initialize + @max_length = 120 + end + + # Shortens text + # + # 116 chars is also where ext-doc makes its cut, but unlike + # ext-doc we only make the cut when there's more than 120 chars. + # + # This way we don't get stupid expansions like: + # + # Blah blah blah some text... + # + # expanding to: + # + # Blah blah blah some text. + # + def shorten(input) + sent = first_sentence(Util::HTML.strip_tags(input).strip) + # Use u-modifier to correctly count multi-byte characters + chars = sent.scan(/./mu) + if chars.length > @max_length + chars[0..(@max_length-4)].join + "..." + else + sent + " ..." + end + end + + # Returns the first sentence inside a string. + def first_sentence(str) + str.sub(/\A(.+?(\.|。))\s.*\z/mu, "\\1") + end + + # Returns true when input should get shortened. + def too_long?(input) + stripped = Util::HTML.strip_tags(input).strip + # for sentence v/s full - compare byte length + # for full v/s max - compare char length + first_sentence(stripped).length < stripped.length || stripped.scan(/./mu).length > @max_length + end + + end + + end +end diff --git a/lib/jsduck/shortener.rb b/lib/jsduck/shortener.rb deleted file mode 100644 index 9312eaf47d16ce4fcf20f00983b0039583be0d01..0000000000000000000000000000000000000000 --- a/lib/jsduck/shortener.rb +++ /dev/null @@ -1,58 +0,0 @@ -# -*- coding: utf-8 -*- -require 'jsduck/util/html' -require 'jsduck/util/singleton' - -module JsDuck - - # Little helper for shortening text - class Shortener - include Util::Singleton - - # Maximum length for text that doesn't get shortened. - # The accessor is used for testing purposes only. - attr_accessor :max_length - - def initialize - @max_length = 120 - end - - # Shortens text - # - # 116 chars is also where ext-doc makes its cut, but unlike - # ext-doc we only make the cut when there's more than 120 chars. - # - # This way we don't get stupid expansions like: - # - # Blah blah blah some text... - # - # expanding to: - # - # Blah blah blah some text. - # - def shorten(input) - sent = first_sentence(Util::HTML.strip_tags(input).strip) - # Use u-modifier to correctly count multi-byte characters - chars = sent.scan(/./mu) - if chars.length > @max_length - chars[0..(@max_length-4)].join + "..." - else - sent + " ..." - end - end - - # Returns the first sentence inside a string. - def first_sentence(str) - str.sub(/\A(.+?(\.|。))\s.*\z/mu, "\\1") - end - - # Returns true when input should get shortened. - def too_long?(input) - stripped = Util::HTML.strip_tags(input).strip - # for sentence v/s full - compare byte length - # for full v/s max - compare char length - first_sentence(stripped).length < stripped.length || stripped.scan(/./mu).length > @max_length - end - - end - -end diff --git a/spec/shortener_spec.rb b/spec/format_shortener_spec.rb similarity index 88% rename from spec/shortener_spec.rb rename to spec/format_shortener_spec.rb index 7985a388c00b01ddc5c59fb38a9673099906314b..aa9a599e70102e82464823f44808c23765d24133 100644 --- a/spec/shortener_spec.rb +++ b/spec/format_shortener_spec.rb @@ -1,16 +1,16 @@ # -*- coding: utf-8 -*- -require "jsduck/shortener" +require "jsduck/format/shortener" -describe JsDuck::Shortener do +describe JsDuck::Format::Shortener do describe "#shorten" do def shorten(text) - JsDuck::Shortener.shorten(text) + JsDuck::Format::Shortener.shorten(text) end before do - JsDuck::Shortener.instance.max_length = 10 + JsDuck::Format::Shortener.instance.max_length = 10 end it "appends ellipsis to short text" do @@ -43,11 +43,11 @@ describe JsDuck::Shortener do describe "#too_long?" do def too_long?(text) - JsDuck::Shortener.too_long?(text) + JsDuck::Format::Shortener.too_long?(text) end before do - JsDuck::Shortener.instance.max_length = 10 + JsDuck::Format::Shortener.instance.max_length = 10 end it "is false when exactly equal to the max_length" do @@ -75,7 +75,7 @@ describe JsDuck::Shortener do describe "#first_sentence" do def first_sentence(text) - JsDuck::Shortener.first_sentence(text) + JsDuck::Format::Shortener.first_sentence(text) end it "extracts first sentence" do