Module: EPUB::CFI

Defined in:
lib/epub/cfi.rb,
lib/epub/cfi/parser.rb,
lib/epub/cfi/version.rb,
lib/epub/cfi/parser.tab.rb

Defined Under Namespace

Classes: CharacterOffset, IDAssertion, Location, Parser, Path, Range, Step, TemporalSpatialOffset, TextLocationAssertion

Constant Summary collapse

SPECIAL_CHARS =

"5E", "5B", "5D", "28", "29", "2C", "3B", "3D"

'^[](),;='
RE_ESCAPED_SPECIAL_CHARS =
Regexp.escape(SPECIAL_CHARS)
VERSION =

epub-cfi version

"0.1.3"

Class Method Summary collapse

Class Method Details

.escape(string) ⇒ String

Escapes special characters in string

Examples:

EPUB::CFI.escape("2[1]") #=> "2^[1^]"

Parameters:

  • string (String)

    Component string of EPUB CFI

Returns:

  • (String)

    Escaped comonent string



16
17
18
# File 'lib/epub/cfi.rb', line 16

def escape(string)
  string.gsub(/([#{RE_ESCAPED_SPECIAL_CHARS}])/o, '^\1')
end

.parse(string) ⇒ CFI

Parses the given string, creates a new EPUB::CFI object and return it.

Examples:

Parses a URI fragment string

EPUB::CFI.parse("epubcfi(/6/14[chap05ref]!/4[body01]/10/2/1:3[2^[1^]])")
#=> #<EPUB::CFI::Location:/6/14[chap05ref]!/4[body01]/10/2/1:3[2^[1^]]>

Parses a path string

EPUB::CFI.parse("/6/14[chap05ref]!/4[body01]/10/2/1:3[2^[1^]]")
#=> #<EPUB::CFI::Location:/6/14[chap05ref]!/4[body01]/10/2/1:3[2^[1^]]>

Parameters:

  • string (String)

Returns:

Raises:

  • (Racc::ParseError)

    if given string is invalid



94
95
96
# File 'lib/epub/cfi/parser.rb', line 94

def parse(string)
  EPUB::CFI::Parser.parse(string)
end

.unescape(string) ⇒ String

Unescape escaped characters in string

Examples:

EPUB::CFI.unescape("2^[1^]") #=> "2[1]"

Parameters:

  • string (String)

    Escape component string of EPUB CFI

Returns:

  • (String)

    Unescaped component string



27
28
29
# File 'lib/epub/cfi.rb', line 27

def unescape(string)
  string.gsub(/\^([#{RE_ESCAPED_SPECIAL_CHARS}])/o, '\1')
end