From 0f22e11055d2caccd5f406bff5ea1ba8600eea56 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Wed, 30 Jan 2013 14:24:41 +0200 Subject: [PATCH] Move Shortener also to Format namespace. --- lib/jsduck/format/class.rb | 6 +- lib/jsduck/format/shortener.rb | 60 +++++++++++++++++++ lib/jsduck/shortener.rb | 58 ------------------ ...tener_spec.rb => format_shortener_spec.rb} | 14 ++--- 4 files changed, 70 insertions(+), 68 deletions(-) create mode 100644 lib/jsduck/format/shortener.rb delete mode 100644 lib/jsduck/shortener.rb rename spec/{shortener_spec.rb => format_shortener_spec.rb} (88%) diff --git a/lib/jsduck/format/class.rb b/lib/jsduck/format/class.rb index 7ba40c61..b2ea6b52 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 00000000..35427710 --- /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 9312eaf4..00000000 --- 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 7985a388..aa9a599e 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 -- GitLab