summary refs log tree commit diff stats
path: root/doc/help
blob: 09ad0c73e5eceaf7d30e2e7b6788dbdb0f8ea1ed (plain) (blame)
1
../ranger/help
t { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
import std/streams

import js/arraybuffer
import js/dict
import js/error
import js/javascript

import chakasu/charset
import chakasu/decoderstream
import chakasu/encoderstream

type
  TextEncoder = ref object

  TextDecoder = ref object
    encoding: Charset
    errorMode: DecoderErrorMode
    ignoreBOM {.jsget.}: bool
    doNotFlush: bool
    bomSeen: bool
    decoder: DecoderStream
    encoder: EncoderStream # to return the string to JS
    istream: StringStream

jsDestructor(TextDecoder)
jsDestructor(TextEncoder)

type TextDecoderOptions = object of JSDict
  fatal: bool
  ignoreBOM: bool

func newTextDecoder(label = "utf-8", options = TextDecoderOptions()):
    JSResult[TextDecoder] {.jsctor.} =
  let errorMode = if options.fatal:
    DECODER_ERROR_MODE_FATAL
  else:
    DECODER_ERROR_MODE_REPLACEMENT
  let encoding = getCharset(label)
  if encoding in {CHARSET_UNKNOWN, CHARSET_REPLACEMENT}:
    return err(newRangeError("Invalid encoding label"))
  return ok(TextDecoder(
    errorMode: errorMode,
    ignoreBOM: options.ignoreBOM,
    encoding: encoding
  ))

type TextDecodeOptions = object of JSDict
  stream: bool

#TODO AllowSharedBufferSource
proc decode(this: TextDecoder, input = none(JSArrayBufferView),
    options = TextDecodeOptions()): string {.jsfunc.} =
  if not this.doNotFlush:
    if this.istream != nil:
      this.istream.close()
    if this.decoder != nil:
      this.decoder.close()
    if this.encoder != nil:
      this.encoder.close()
    this.istream = newStringStream()
    this.decoder = newDecoderStream(this.istream, cs = this.encoding,
      errormode = this.errorMode)
    this.encoder = newEncoderStream(this.decoder, cs = CHARSET_UTF_8)
    this.bomSeen = false
  if this.doNotFlush != options.stream:
    this.doNotFlush = options.stream
    this.decoder.setInhibitCheckEnd(options.stream)
  if input.isSome:
    let input = input.get
    let pos = this.istream.getPosition()
    #TODO input offset?
    this.istream.writeData(input.abuf.p, int(input.abuf.len))
    this.istream.setPosition(pos)
  #TODO this should return a JSString, so we do not needlessly re-encode
  # the output. (Right now we do, implicitly through toJS.)
  return this.encoder.readAll()

func jencoding(this: TextDecoder): string {.jsfget: "encoding".} =
  return $this.encoding

func fatal(this: TextDecoder): bool {.jsfget.} =
  return this.errorMode == DECODER_ERROR_MODE_FATAL

func newTextEncoder(): TextEncoder {.jsctor.} =
  return TextEncoder()

func jencoding(this: TextEncoder): string {.jsfget: "encoding".} =
  return "utf-8"

proc dealloc_wrap(rt: JSRuntime, opaque, p: pointer) {.cdecl.} =
  dealloc(p)

proc encode(this: TextEncoder, input = ""): JSUint8Array {.jsfunc.} =
  # input is already UTF-8 here :P
  let buf = cast[ptr UncheckedArray[uint8]](alloc(input.len))
  copyMem(buf, unsafeAddr input[0], input.len)
  let abuf = JSArrayBuffer(
    p: buf,
    len: csize_t(input.len),
    dealloc: dealloc_wrap
  )
  return JSUint8Array(
    abuf: abuf,
    offset: 0,
    nmemb: csize_t(input.len)
  )

#TODO encodeInto

proc addEncodingModule*(ctx: JSContext) =
  ctx.registerType(TextDecoder)
  ctx.registerType(TextEncoder)