text:
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.prestosql.plugin.iceberg;

import com.google.common.collect.ImmutableList;
import io.airlift.json.JsonCodec;
import io.airlift.slice.Slice;
import io.prestosql.plugin.hive.HdfsEnvironment;
import io.prestosql.plugin.hive.HdfsEnvironment.HdfsContext;
import io.prestosql.plugin.iceberg.PartitionTransforms.ColumnTransform;
import io.prestosql.spi.Page;
import io.prestosql.spi.PageIndexer;
import io.prestosql.spi.PageIndexerFactory;
import io.prestosql.spi.PrestoException;
import io.prestosql.spi.block.Block;
import io.prestosql.spi.connector.ConnectorPageSink;
import io.prestosql.spi.connector.ConnectorSession;
import io.prestosql.spi.type.BigintType;
import io.prestosql.spi.type.BooleanType;
import io.prestosql.spi.type.DateType;
import io.prestosql.spi.type.DecimalType;
import io.prestosql.spi.type.DoubleType;
import io.prestosql.spi.type.IntegerType;
import io.prestosql.spi.type.RealType;
import io.prestosql.spi.type.SmallintType;
import io.prestosql.spi.type.TinyintType;
import io.prestosql.spi.type.Type;
import io.prestosql.spi.type.VarbinaryType;
import io.prestosql.spi.type.VarcharType;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapred.JobConf;
import org.apache.iceberg.FileFormat;
import org.apache.iceberg.PartitionField;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.Schema;
import org.apache.iceberg.transforms.Transform;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Verify.verify;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.airlift.slice.Slices.wrappedBuffer;
import static io.prestosql.plugin.hive.util.ConfigurationUtils.toJobConf;
import static io.prestosql.plugin.iceberg.IcebergErrorCode.ICEBERG_TOO_MANY_OPEN_PARTITIONS;
import static io.prestosql.plugin.iceberg.PartitionTransforms.getColumnTransform;
import static io.prestosql.plugin.iceberg.util.Timestamps.getTimestampTz;
import static io.prestosql.plugin.iceberg.util.Timestamps.timestampTzToMicros;
import static io.prestosql.spi.type.Decimals.readBigDecimal;
import static io.prestosql.spi.type.TimeType.TIME_MICROS;
import static io.prestosql.spi.type.TimestampType.TIMESTAMP_MICROS;
import static io.prestosql.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_MICROS;
import static io.prestosql.spi.type.Timestamps.PICOSECONDS_PER_MICROSECOND;
import static java.lang.Float.intBitsToFloat;
import static java.lang.Math.toIntExact;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.UUID.randomUUID;
import static java.util.concurrent.CompletableFuture.completedFuture;

public class IcebergPageSink
        implements ConnectorPageSink
{
    private static final int MAX_PAGE_POSITIONS = 4096;

    @SuppressWarnings({"FieldCanBeLocal", "FieldMayBeStatic"})
    private final int maxOpenWriters = 100;  // TODO: make this configurable
    private final Schema outputSchema;
    private final PartitionSpec partitionSpec;
    private final String outputPath;
    private final IcebergFileWriterFactory fileWriterFactory;
    private final HdfsEnvironment hdfsEnvironment;
    private final HdfsContext hdfsContext;
    private final JobConf jobConf;
    private final JsonCodec<CommitTaskData> jsonCodec;
    private final ConnectorSession session;
    private final FileFormat fileFormat;
    private final PagePartitioner pagePartitioner;

    private final List<WriteContext> writers = new ArrayList<>();

    private long writtenBytes;
    private long systemMemoryUsage;
    private long validationCpuNanos;

    public IcebergPageSink(
            Schema outputSchema,
            PartitionSpec partitionSpec,
            String outputPath,
            IcebergFileWriterFactory fileWriterFactory,
            PageIndexerFactory pageIndexerFactory,
            HdfsEnvironment hdfsEnvironment,
            HdfsContext hdfsContext,
            List<IcebergColumnHandle> inputColumns,
            JsonCodec<CommitTaskData> jsonCodec,
            ConnectorSession session,
            FileFormat fileFormat)
    {
        requireNonNull(inputColumns, "inputColumns is null");
        this.outputSchema = requireNonNull(outputSchema, "outputSchema is null");
        this.partitionSpec = requireNonNull(partitionSpec, "partitionSpec is null");
        this.outputPath = requireNonNull(outputPath, "outputPath is null");
        this.fileWriterFactory = requireNonNull(fileWriterFactory, "fileWriterFactory is null");
        this.hdfsEnvironment = requireNonNull(hdfsEnvironment, "hdfsEnvironment is null");
        this.hdfsContext = requireNonNull(hdfsContext, "hdfsContext is null");
        this.jobConf = toJobConf(hdfsEnvironment.getConfiguration(hdfsContext, new Path(outputPath)));
        this.jsonCodec = requireNonNull(jsonCodec, "jsonCodec is null");
        this.session = requireNonNull(session, "session is null");
        this.fileFormat = requireNonNull(fileFormat, "fileFormat is null");
        this.pagePartitioner = new PagePartitioner(pageIndexerFactory, toPartitionColumns(inputColumns, partitionSpec));
    }

    @Override
    public long getCompletedBytes()
    {
        return writtenBytes;
    }

    @Override
    public long getSystemMemoryUsage()
    {
        return systemMemoryUsage;
    }

    @Override
    public long getValidationCpuNanos()
    {
        return validationCpuNanos;
    }

    @Override
    public CompletableFuture<?> appendPage(Page page)
    {
        hdfsEnvironment.doAs(session.getUser(), () -> doAppend(page));

        return NOT_BLOCKED;
    }

    @Override
    public CompletableFuture<Collection<Slice>> finish()
    {
        Collection<Slice> commitTasks = new ArrayList<>();

        for (WriteContext context : writers) {
            context.getWriter().commit();

            CommitTaskData task = new CommitTaskData(
                    context.getPath().toString(),
                    new MetricsWrapper(context.writer.getMetrics()),
                    context.getPartitionData().map(PartitionData::toJson));

            commitTasks.add(wrappedBuffer(jsonCodec.toJsonBytes(task)));
        }

        writtenBytes = writers.stream()
                .mapToLong(writer -> writer.getWriter().getWrittenBytes())
                .sum();
        validationCpuNanos = writers.stream()
                .mapToLong(writer -> writer.getWriter().getValidationCpuNanos())
                .sum();

        return completedFuture(commitTasks);
    }

    @Override
    public void abort()
    {
        RuntimeException error = null;
        for (WriteContext context : writers) {
            try {
                if (context != null) {
                    context.getWriter().rollback();
                }
            }
            catch (Throwable t) {
                if (error == null) {
                    error = new RuntimeException("Exception during rollback");
                }
                error.addSuppressed(t);
            }
        }
        if (error != null) {
            throw error;
        }
    }

    private void doAppend(Page page)
    {
        while (page.getPositionCount() > MAX_PAGE_POSITIONS) {
            Page chunk = page.getRegion(0, MAX_PAGE_POSITIONS);
            page = page.getRegion(MAX_PAGE_POSITIONS, page.getPositionCount() - MAX_PAGE_POSITIONS);
            writePage(chunk);
        }

        writePage(page);
    }

    private void writePage(Page page)
    {
        int[] writerIndexes = getWriterIndexes(page);

        // position count for each writer
        int[] sizes = new int[writers.size()];
        for (int index : writerIndexes) {
            sizes[index]++;
        }

        // record which positions are used by which writer
        int[][] writerPositions = new int[writers.size()][];
        int[] counts = new int[writers.size()];

        for (int position = 0; position < page.getPositionCount(); position++) {
            int index = writerIndexes[position];

            int count = counts[index];
            if (count == 0) {
                writerPositions[index] = new int[sizes[index]];
            }
            writerPositions[index][count] = position;
            counts[index]++;
        }

        // invoke the writers
        for (int index = 0; index < writerPositions.length; index++) {
            int[] positions = writerPositions[index];
            if (positions == null) {
                continue;
            }

            // if write is partitioned across multiple writers, filter page using dictionary blocks
            Page pageForWriter = page;
            if (positions.length != page.getPositionCount()) {
                verify(positions.length == counts[index]);
                pageForWriter = pageForWriter.getPositions(positions, 0, positions.length);
            }

            IcebergFileWriter writer = writers.get(index).getWriter();

            long currentWritten = writer.getWrittenBytes();
            long currentMemory = writer.getSystemMemoryUsage();

            writer.appendRows(pageForWriter);

            writtenBytes += (writer.getWrittenBytes() - currentWritten);
            systemMemoryUsage += (writer.getSystemMemoryUsage() - currentMemory);
        }
    }

    private int[] getWriterIndexes(Page page)
    {
        int[] writerIndexes = pagePartitioner.partitionPage(page);

        if (pagePartitioner.getMaxIndex() >= maxOpenWriters) {
            throw new PrestoException(ICEBERG_TOO_MANY_OPEN_PARTITIONS, format("Exceeded limit of %s open writers for partitions", maxOpenWriters));
        }

        // expand writers list to new size
        while (writers.size() <= pagePartitioner.getMaxIndex()) {
            writers.add(null);
        }

        // create missing writers
        for (int position = 0; position < page.getPositionCount(); position++) {
            int writerIndex = writerIndexes[position];
            if (writers.get(writerIndex) != null) {
                continue;
            }

            Optional<PartitionData> partitionData = getPartitionData(pagePartitioner.getColumns(), page, position);
            Optional<String> partitionPath = partitionData.map(partitionSpec::partitionToPath);

            WriteContext writer = createWriter(partitionPath, partitionData);

            writers.set(writerIndex, writer);
        }
        verify(writers.size() == pagePartitioner.getMaxIndex() + 1);
        verify(!writers.contains(null));

        return writerIndexes;
    }

    private WriteContext createWriter(Optional<String> partitionPath, Optional<PartitionData> partitionData)
    {
        Path outputPath = new Path(this.outputPath);
        if (partitionPath.isPresent()) {
            outputPath = new Path(outputPath, partitionPath.get());
        }
        outputPath = new Path(outputPath, randomUUID().toString());
        outputPath = new Path(fileFormat.addExtension(outputPath.toString()));

        IcebergFileWriter writer = fileWriterFactory.createFileWriter(
                outputPath,
                outputSchema,
                jobConf,
                session,
                hdfsContext,
                fileFormat);

        return new WriteContext(writer, outputPath, partitionData);
    }

    private static Optional<PartitionData> getPartitionData(List<PartitionColumn> columns, Page page, int position)
    {
        if (columns.isEmpty()) {
            return Optional.empty();
        }

        Object[] values = new Object[columns.size()];
        for (int i = 0; i < columns.size(); i++) {
            PartitionColumn column = columns.get(i);
            Block block = page.getBlock(column.getSourceChannel());
            Type type = column.getSourceType();
            Object value = getIcebergValue(block, position, type);
            values[i] = applyTransform(column.getField().transform(), value);
        }
        return Optional.of(new PartitionData(values));
    }

    @SuppressWarnings("unchecked")
    private static Object applyTransform(Transform<?, ?> transform, Object value)
    {
        return ((Transform<Object, Object>) transform).apply(value);
    }

    public static Object getIcebergValue(Block block, int position, Type type)
    {
        if (block.isNull(position)) {
            return null;
        }
        if (type instanceof BigintType) {
            return type.getLong(block, position);
        }
        if (type instanceof IntegerType || type instanceof SmallintType || type instanceof TinyintType || type instanceof DateType) {
            return toIntExact(type.getLong(block, position));
        }
        if (type instanceof BooleanType) {
            return type.getBoolean(block, position);
        }
        if (type instanceof DecimalType) {
            return readBigDecimal((DecimalType) type, block, position);
        }
        if (type instanceof RealType) {
            return intBitsToFloat(toIntExact(type.getLong(block, position)));
        }
        if (type instanceof DoubleType) {
            return type.getDouble(block, position);
        }
        if (type.equals(TIME_MICROS)) {
            return type.getLong(block, position) / PICOSECONDS_PER_MICROSECOND;
        }
        if (type.equals(TIMESTAMP_MICROS)) {
            return type.getLong(block, position);
        }
        if (type.equals(TIMESTAMP_TZ_MICROS)) {
            return timestampTzToMicros(getTimestampTz(block, position));
        }
        if (type instanceof VarbinaryType) {
            return type.getSlice(block, position).getBytes();
        }
        if (type instanceof VarcharType) {
            return type.getSlice(block, position).toStringUtf8();
        }
        throw new UnsupportedOperationException("Type not supported as partition column: " + type.getDisplayName());
    }

    private static List<PartitionColumn> toPartitionColumns(List<IcebergColumnHandle> handles, PartitionSpec partitionSpec)
    {
        Map<Integer, Integer> idChannels = new HashMap<>();
        for (int i = 0; i < handles.size(); i++) {
            idChannels.put(handles.get(i).getId(), i);
        }

        return partitionSpec.fields().stream()
                .map(field -> {
                    Integer channel = idChannels.get(field.sourceId());
                    checkArgument(channel != null, "partition field not found: %s", field);
                    Type inputType = handles.get(channel).getType();
                    ColumnTransform transform = getColumnTransform(field, inputType);
                    return new PartitionColumn(field, channel, inputType, transform.getType(), transform.getTransform());
                })
                .collect(toImmutableList());
    }

    private static class WriteContext
    {
        private final IcebergFileWriter writer;
        private final Path path;
        private final Optional<PartitionData> partitionData;

        public WriteContext(IcebergFileWriter writer, Path path, Optional<PartitionData> partitionData)
        {
            this.writer = requireNonNull(writer, "writer is null");
            this.path = requireNonNull(path, "path is null");
            this.partitionData = requireNonNull(partitionData, "partitionData is null");
        }

        public IcebergFileWriter getWriter()
        {
            return writer;
        }

        public Path getPath()
        {
            return path;
        }

        public Optional<PartitionData> getPartitionData()
        {
            return partitionData;
        }
    }

    private static class PagePartitioner
    {
        private final PageIndexer pageIndexer;
        private final List<PartitionColumn> columns;

        public PagePartitioner(PageIndexerFactory pageIndexerFactory, List<PartitionColumn> columns)
        {
            this.pageIndexer = pageIndexerFactory.createPageIndexer(columns.stream()
                    .map(PartitionColumn::getResultType)
                    .collect(toImmutableList()));
            this.columns = ImmutableList.copyOf(columns);
        }

        public int[] partitionPage(Page page)
        {
            Block[] blocks = new Block[columns.size()];
            for (int i = 0; i < columns.size(); i++) {
                PartitionColumn column = columns.get(i);
                Block block = page.getBlock(column.getSourceChannel());
                blocks[i] = column.getBlockTransform().apply(block);
            }
            Page transformed = new Page(page.getPositionCount(), blocks);

            return pageIndexer.indexPage(transformed);
        }

        public int getMaxIndex()
        {
            return pageIndexer.getMaxIndex();
        }

        public List<PartitionColumn> getColumns()
        {
            return columns;
        }
    }

    private static class PartitionColumn
    {
        private final PartitionField field;
        private final int sourceChannel;
        private final Type sourceType;
        private final Type resultType;
        private final Function<Block, Block> blockTransform;

        public PartitionColumn(PartitionField field, int sourceChannel, Type sourceType, Type resultType, Function<Block, Block> blockTransform)
        {
            this.field = requireNonNull(field, "field is null");
            this.sourceChannel = sourceChannel;
            this.sourceType = requireNonNull(sourceType, "sourceType is null");
            this.resultType = requireNonNull(resultType, "resultType is null");
            this.blockTransform = requireNonNull(blockTransform, "blockTransform is null");
        }

        public PartitionField getField()
        {
            return field;
        }

        public int getSourceChannel()
        {
            return sourceChannel;
        }

        public Type getSourceType()
        {
            return sourceType;
        }

        public Type getResultType()
        {
            return resultType;
        }

        public Function<Block, Block> getBlockTransform()
        {
            return blockTransform;
        }
    }
}


text:
const footer = require('../readme-footer');

var readme = function(name, options={}) {
  const {description='', homepage='http://styleguide.pivotal.io'} = options;

  return `# pui-react-${name}
${description}

This component requires React

See the [Pivotal UI Styleguide](${homepage}) for fully rendered examples.

${footer()}`;
};

module.exports = readme;

redpajama_set_name: RedPajamaGithub


text:
int func_from_language_runtime(void);
typedef int (*fptr) (void);

#ifdef _WIN32

#include <windows.h>

static wchar_t*
win32_get_last_error (void)
{
    wchar_t *msg = NULL;

    FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
                    | FORMAT_MESSAGE_IGNORE_INSERTS
                    | FORMAT_MESSAGE_FROM_SYSTEM,
                    NULL, GetLastError (), 0,
                    (LPWSTR) &msg, 0, NULL);
    return msg;
}

int main(int argc, char **argv)
{
    HINSTANCE handle;
    fptr importedfunc;
    int expected, actual;
    int ret = 1;
    if(argc==0) {};

    handle = LoadLibraryA (argv[1]);
    if (!handle) {
        wchar_t *msg = win32_get_last_error ();
        printf ("Could not open %s: %S\n", argv[1], msg);
        goto nohandle;
    }

    importedfunc = (fptr) GetProcAddress (handle, "func");
    if (importedfunc == NULL) {
        wchar_t *msg = win32_get_last_error ();
        printf ("Could not find 'func': %S\n", msg);
        goto out;
    }

    actual = importedfunc ();
    expected = func_from_language_runtime ();
    if (actual != expected) {
        printf ("Got %i instead of %i\n", actual, expected);
        goto out;
    }

    ret = 0;
out:
    FreeLibrary (handle);
nohandle:
    return ret;
}

#else

#include<dlfcn.h>
#include<assert.h>

int main(int argc, char **argv) {
    void *dl;
    fptr importedfunc;
    int expected, actual;
    char *error;
    int ret = 1;
    if(argc==0) {};

    dlerror();
    dl = dlopen(argv[1], RTLD_LAZY);
    error = dlerror();
    if(error) {
        printf("Could not open %s: %s\n", argv[1], error);
        goto nodl;
    }

    importedfunc = (fptr) dlsym(dl, "func");
    if (importedfunc == NULL) {
        printf ("Could not find 'func'\n");
        goto out;
    }

    assert(importedfunc != func_from_language_runtime);

    actual = (*importedfunc)();
    expected = func_from_language_runtime ();
    if (actual != expected) {
        printf ("Got %i instead of %i\n", actual, expected);
        goto out;
    }

    ret = 0;
out:
    dlclose(dl);
nodl:
    return ret;
}

#endif


text:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace Tiraggo.DynamicQuery
{
    /// <summary>
    /// Used to house the parameters to Query.Select()
    /// </summary>
    [Serializable]
    [DataContract(Namespace = "tg", IsReference = true)]
    public class tgExpression
    {
        public tgExpression() { }

        /// <summary>
        /// Back Pointer to the Parent Query
        /// </summary>
        [DataMember(Name = "ParentQuery", Order = 99, EmitDefaultValue = false)]
        public tgDynamicQuerySerializable Query;

        /// <summary>
        /// Contains the necessary information to describe this column
        /// </summary>
        [DataMember(Name = "Column", EmitDefaultValue = false)]
        public tgColumnItem Column;

        /// <summary>
        /// A collection of SubOperators such as ToLower to apply to the select column
        /// </summary>
        [DataMember(Name = "SubOperators", EmitDefaultValue = false)]
        public List<tgQuerySubOperator> SubOperators;

        /// <summary>
        /// Case / When / Then / End
        /// </summary>
        [DataMember(Name = "CaseWhen", EmitDefaultValue = false)]
        public tgCase CaseWhen;

        /// <summary>
        /// The data behind the expression. This ends up looking like a tree in the end as
        /// more arithmetic expressions are applied
        /// </summary>
        [DataMember(Name = "Expression", EmitDefaultValue = false)]
        public tgMathmaticalExpression MathmaticalExpression;

        /// <summary>
        ///
        /// </summary>
        [DataMember(Name = "LiteralValue", EmitDefaultValue = false)]
        public object LiteralValue;

        /// <summary>
        /// True if this tgExpression has an expression formed by the arithmetic operators +,-,*,/,%
        /// </summary>
        public bool HasMathmaticalExpression
        {
            get { return this.MathmaticalExpression != null; }
        }

        /// <summary>
        /// True if this tgExpression merely represents a literal value
        /// </summary>
        public bool IsLiteralValue
        {
            get { return this.LiteralValue != null; }
        }
    }
}


text:
#pragma once
#include <aws/lex-models/LexModelBuildingService_EXPORTS.h>
#include <aws/lex-models/model/ReferenceType.h>
#include <aws/lex-models/model/ResourceReference.h>
#include <utility>

namespace Aws
{
namespace Utils
{
namespace Json
{
  class JsonValue;
  class JsonView;
} // namespace Json
} // namespace Utils
namespace LexModelBuildingService
{
namespace Model
{

  /**
   *
The resource that you are attempting to delete is referred to by another
   * resource. Use this information to remove references to the resource that you are
   * trying to delete.

The body of the exception contains a JSON object that
   * describes the resource.

 { "resourceType": BOT | BOTALIAS |
   * BOTCHANNEL | INTENT,

 "resourceReference": {

   *
 "name": string, "version": string } }
   *

See Also:
   AWS
   * API Reference

   */
  class AWS_LEXMODELBUILDINGSERVICE_API ResourceInUseException
  {
  public:
    ResourceInUseException();
    ResourceInUseException(Aws::Utils::Json::JsonView jsonValue);
    ResourceInUseException& operator=(Aws::Utils::Json::JsonView jsonValue);
    Aws::Utils::Json::JsonValue Jsonize() const;

    inline const ReferenceType& GetReferenceType() const{ return m_referenceType; }

    inline bool ReferenceTypeHasBeenSet() const { return m_referenceTypeHasBeenSet; }

    inline void SetReferenceType(const ReferenceType& value) { m_referenceTypeHasBeenSet = true; m_referenceType = value; }

    inline void SetReferenceType(ReferenceType&& value) { m_referenceTypeHasBeenSet = true; m_referenceType = std::move(value); }

    inline ResourceInUseException& WithReferenceType(const ReferenceType& value) { SetReferenceType(value); return *this;}

    inline ResourceInUseException& WithReferenceType(ReferenceType&& value) { SetReferenceType(std::move(value)); return *this;}

    inline const ResourceReference& GetExampleReference() const{ return m_exampleReference; }

    inline bool ExampleReferenceHasBeenSet() const { return m_exampleReferenceHasBeenSet; }

    inline void SetExampleReference(const ResourceReference& value) { m_exampleReferenceHasBeenSet = true; m_exampleReference = value; }

    inline void SetExampleReference(ResourceReference&& value) { m_exampleReferenceHasBeenSet = true; m_exampleReference = std::move(value); }

    inline ResourceInUseException& WithExampleReference(const ResourceReference& value) { SetExampleReference(value); return *this;}

    inline ResourceInUseException& WithExampleReference(ResourceReference&& value) { SetExampleReference(std::move(value)); return *this;}

  private:

    ReferenceType m_referenceType;
    bool m_referenceTypeHasBeenSet;

    ResourceReference m_exampleReference;
    bool m_exampleReferenceHasBeenSet;
  };

} // namespace Model
} // namespace LexModelBuildingService
} // namespace Aws


text:
var _          = require('underscore');     // collections helper
var assert     = require('assert');         // assertions
var helper     = require('meta4common');   // files & mixins
var debug      = require("../../../debug")("crud:loki");
var paths      = require("path");

// =============================================================================
// Feature Packages

var loki       = require('lokijs');

var self = exports;
self.idAttribute = "$loki";
self._db = {};

// =============================================================================
// safely acquire a Collection

self.getCollection = function(crud, db) {
	// get our collection
	var collection = db.getCollection( crud.id );
	if (!collection) {
		// if not, create it
		collection = db.addCollection( crud.id );
	};

	return collection
}

self.acquireDatabase = function(crud, cb) {
    assert(crud, "Missing CRUD");
    assert(crud.id, "Missing CRUD id");
    assert(crud.idAttribute, "Missing LOKI idAttribute");

    assert(cb, "Missing CRUD callback: "+crud.id);
    assert(_.isFunction(cb), "Missing CRUD callback: "+crud.id);

	var home = crud.adapter.home || crud.home;
    assert(home, "Loki CRUD @ "+crud.id+" is missing home");

    var DEBUG = crud.debug?true:false;

	// underlying database

	var db = exports._db[crud.id]
	if (db) {
		// already initialized ..
		cb && cb(null, db)
	    return;
	}

	// initialize database
	var autosaveInterval = crud.adapter.autosaveInterval?crud.adapter.autosaveInterval:3000

	// file management
//	crud.adapter.database.name
    helper.files.mkdirs(home);
	var filename = paths.resolve(paths.normalize(home+"/"+crud.id+".json"));
    DEBUG && debug("%s @ %j --> %s", crud.id, home, filename);

    // load Loki - callback when done
    db = exports._db[crud.id] = new loki( filename, { autoload: true, autosave: true, autosaveInterval: autosaveInterval,
        autoloadCallback: function() {
            DEBUG && debug("acquired: %s", crud.id); // , db.collections, arguments
	        cb && cb(null, db);
        }
     } );
}

// =============================================================================
// Create

exports.create = function(crud, cmd, cb) {

    var DEBUG = crud.debug;

	self.acquireDatabase(crud, function(err, db) {
		if (err) {
			cb && cb( { status: "failed", err: err });
			return false;
		}

//		debug("create:",crud.id, cmd.data);

        var collection = self.getCollection(crud, db);
		var found = collection.insert(cmd.data)

		// externalize ID attribute
        DEBUG && debug("created %s -> %j",crud.id, found);

		// we're done
		cb && cb(null, { status: "success", data: [cmd.data], meta: {  } })

	})

}

// =============================================================================
// Read / GET

exports.read = function(crud, cmd, cb) {
    assert(crud, "Missing CRUD");
    assert(cmd, "Missing command");
    assert(cb, "Missing callback");
    assert(_.isFunction(cb), "Invalid callback: "+JSON.stringify(cb));
    assert(crud.adapter, "Missing adapter");
    assert(crud.adapter.type, "Invalid adapter");
    assert(crud.adapter.home, "Missing loki home");

    var DEBUG = crud.debug?true:false;

	self.acquireDatabase(crud, function(err, db) {

		if (err) {
			cb && cb( { status: "failed" , err: err });
			return false
		}
		assert(db, "Missing loki database: "+crud.id);

  		var collection = self.getCollection(crud, db);
        if (!collection) {
            cb && cb( { status: "failed" , err: "missing "+crud.id } );
            return;
        }
        assert(collection.name==crud.id, "Collection name mismatched: "+collection.name);

        var where = _.extend({}, cmd.where, crud.where);
        var filters = _.extend({}, cmd.filters, crud.filters);

        DEBUG && debug("read %s", crud.id);
        var found = where?collection.find(where):collection.data;
        found = found || [];
        DEBUG && debug("found %s x %s", found.length, crud.id);

		// we're done
		cb && cb( null, { status: "success", data: found });
	})
}

// =============================================================================
// Update / PUT

exports.update = function(crud, cmd, cb) {

    var DEBUG = crud.debug?true:false;

    self.acquireDatabase(crud, function(err, db) {

		if (err) {
			cb && cb( { status: "failed", err: err });
			return false
		}

        DEBUG && debug("update:", crud.id, cmd.data)

        var collection = self.getCollection(crud, db);
        DEBUG && debug("update %s -> %j", crud.id, cmd.data);

		var found = collection.update(cmd.data);
		// we're done
		cb && cb( null, { status: "success", data: [cmd.data], meta: { count: found } });
	})

}

// =============================================================================
// Delete / DELETE

exports.delete = function(crud, cmd, cb) {

    var DEBUG = crud.debug?true:false;

    self.acquireDatabase(crud, function(err, db) {

		if (err) {
			cb && cb( { status: "failed", err: err });
			return false
		}

        DEBUG && debug("delete: %s -> %s", crud.id, cmd.data[crud.idAttribute]);

        var collection = self.getCollection(crud, db);
		var found = collection.removeWhere(cmd.data)
        DEBUG && debug("deleted: %s -> %j",crud.id, found)

		// we're done
		cb & cb( null, { status: "success", data: [], meta: { deleted: found?found:false } });
	})
}

// =============================================================================
// Initial Install

exports.install = function(crud, feature, cb) {
    assert(crud, "Missing CRUD");
    assert(feature, "Missing CRUD feature: "+crud.id);
    assert(cb, "Missing CRUD callback: "+crud.id);
    assert(_.isFunction(cb), "Invalid CRUD callback: "+crud.id);

    var DEBUG = crud.debug?true:false;

    if (!crud.defaults) {
         return;
    };

    self.acquireDatabase(crud, function(err, db) {
        if (err) {
            cb && cb( err, { status: "failed", err: err });
            return false
        }

        crud.defaults = crud.defaults || [];
        var collection = self.getCollection(crud, db);
        if (!collection.count()) {
            DEBUG && debug("installing: %s from %o ", crud.id, crud.defaults);

            if (_.isString(crud.defaults)) {
                helper.files.parse(crud.defaults, function(file, json) {
                    //DEBUG &&
                    debug("installed x %s models from : %s as %s", _.keys(json).length, file, crud.id);
                    _.each(json, function(data) {
                        collection.insert(data);
                    });
                });
            }

            if (_.isArray(crud.defaults)) {
                _.each(crud.defaults, function(data) {
                    collection.insert(data);
                });
                DEBUG && debug("installed: %s x %s models", _.keys(crud.defaults).length, crud.id);
            }
        }

        // we're done
        cb && cb(null, { status: "pending", data: crud.defaults, meta: {  } })
    })
}

return self;


text:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <TITLE>Generic Image Library: planar_pixel_iterator.hpp File Reference</TITLE>
    <META HTTP-EQUIV="content-type" CONTENT="text/html;charset=ISO-8859-1"/>
    <LINK TYPE="text/css" REL="stylesheet" HREF="adobe_source.css"/>

Modules
                  | Alphabetical List
                  | Class List
                  | Directories
                  | File List
                  | GIL Home Page

boost &raquo gil

planar_pixel_iterator.hpp File Reference

Detailed Description

planar pixel pointer class

Author:

Lubomir Bourdev and Hailin Jin

 Adobe Systems Incorporated

Date:

2005-2007

 Last updated on February 12, 2007

#include <cassert>

#include <iterator>

#include <boost/iterator/iterator_facade.hpp>

#include "gil_config.hpp"

#include "pixel.hpp"

#include "step_iterator.hpp"

Go to the source code of this file.

Namespaces

namespace  	boost

namespace  	boost::gil

namespace  	boost::gil::detail

Classes

struct  	planar_pixel_iterator

 	An iterator over planar pixels. Models HomogeneousColorBaseConcept, PixelIteratorConcept, HomogeneousPixelBasedConcept, MemoryBasedIteratorConcept, HasDynamicXStepTypeConcept.  More...

Functions

template<typename IC, typename C>

std::ptrdiff_t 	memunit_step (const planar_pixel_iterator< IC, C > &)

template<typename IC, typename C>

std::ptrdiff_t 	memunit_distance (const planar_pixel_iterator< IC, C > &p1, const planar_pixel_iterator< IC, C > &p2)

template<typename IC, typename C>

void 	memunit_advance (planar_pixel_iterator< IC, C > &p, std::ptrdiff_t diff)

template<typename IC, typename C>

planar_pixel_iterator< IC, C > 	memunit_advanced (const planar_pixel_iterator< IC, C > &p, std::ptrdiff_t diff)

template<typename ChannelPtr, typename ColorSpace>

planar_pixel_reference

< typename

std::iterator_traits

< ChannelPtr >::reference,

ColorSpace > 	memunit_advanced_ref (const planar_pixel_iterator< ChannelPtr, ColorSpace > &ptr, std::ptrdiff_t diff)

<address style="text-align: right;">Generated on Sat May 2 13:50:16 2009 for Generic Image Library by

 1.5.6 </address>


text:
package eta.runtime.thunk;

import java.util.Stack;
import java.util.ListIterator;
import java.util.concurrent.atomic.AtomicReference;

import eta.runtime.RtsFlags;
import eta.runtime.stg.Capability;
import eta.runtime.stg.StgTSO;
import eta.runtime.stg.StgContext;
import eta.runtime.stg.StackFrame;
import eta.runtime.stg.StgEnter;
import eta.runtime.stg.StgClosure;
import eta.runtime.stg.StgAPStack;
import eta.runtime.exception.StgRaise;
import static eta.runtime.stg.StackFrame.MarkFrameResult.Default;
import static eta.runtime.stg.StackFrame.MarkFrameResult.Marked;

public abstract class UpdateFrame extends StackFrame {
    public final StgThunk updatee;
    public volatile boolean marked;

    public UpdateFrame(final StgThunk updatee) {
        this.updatee = updatee;
    }

    @Override
    public void stackEnter(StgContext context) {
        StgClosure ret = context.R(1);
        StgClosure v = updatee.indirectee;
        StgTSO tso = context.currentTSO;
        Capability cap = context.myCapability;
        if (v.getEvaluated() != null) {
            cap.checkBlockingQueues(tso);
            context.R(1, v);
        } else if (v == tso) {
            updatee.updateWithIndirection(ret);
        } else {
            cap.updateThunk(tso, updatee, ret);
            context.R(1, ret);
        }
    }

    @Override
    public boolean doRaiseAsync(Capability cap, StgTSO tso, StgClosure exception, boolean stopAtAtomically, StgThunk updatee, AtomicReference<StgClosure> topClosure) {
        Stack<StackFrame> stack = new Stack<StackFrame>();
        ListIterator<StackFrame> sp = tso.sp;
        sp.next(); //Shift to above the update frame
        do {
            stack.push(sp.next());
            sp.remove();
        } while (sp.hasNext());
        StgClosure fun = topClosure.get();
        StgClosure ap = new StgAPStack(fun, stack);
        if (this.updatee == updatee) {
            ap = updatee;
        } else {
            cap.updateThunk(tso, this.updatee, ap);
        }
        tso.spPop();
        topClosure.set(ap);
        return true;
    }

    @Override
    public boolean doRaiseExceptionHelper(Capability cap, StgTSO tso, AtomicReference<StgClosure> raiseClosure, StgClosure exception) {
        StgClosure raise = raiseClosure.get();
        if (raise == null) {
            raise = new StgRaise(exception);
            raiseClosure.set(raise);
        }
        cap.updateThunk(tso, updatee, raise);
        return true;
    }

    @Override
    public MarkFrameResult mark(Capability cap, StgTSO tso) {
        if (marked) {
            return Marked;
        } else {
            marked = true;
            StgThunk bh = updatee;
            do {
                StgClosure oldIndirectee = bh.indirectee;
                // TODO: This check only applies for black and white holes. What
                //       happens if it's a BlockingQueue?
                if (bh.getEvaluated() == null && bh.indirectee != null &&
                    bh.indirectee != tso) {
                    cap.suspendComputation(tso, this);
                    // TODO: Verify that suspendComputation deletes all frames above
                    //       this one.
                    assert  tso.sp.hasNext() == false
                          : "mark: suspended computation with extra stack frames.";
                    tso.spPush(new StgEnter(bh));
                    tso.spPrevious();
                    return Default;
                } else {
                    if (RtsFlags.ModeFlags.threaded) {
                        if (!bh.tryLock(oldIndirectee)) {
                            continue;
                        }
                    }
                    bh.updateWithIndirection(tso);
                    return Default;
                }
            } while (true);
        }
    }
}


text:
<?php

namespace Lull\Client\Adapter;

use \Doctrine\Common\Cache\Cache;

interface AdapterInterface
{
    /**
     * @param string $resourceName
     * @param array $conditions
     * @return mixed
     */
    public function get($resourceName, array $conditions = array());

    /**
     * @param string $baseUrl
     */
    public function setBaseUrl($baseUrl);

    /**
     * @return string
     */
    public function getBaseUrl();

    /**
     * @param array $options
     * @return void
     */
    public function setOptions(array $options);

    /**
     * @preturn string
     */
    public function getOptions();

    /**
     * @param \Doctrine\Common\Cache\Cache $cache
     * @return void
     */
    public function setCache(Cache $cache);

    /**
     * @return string
     */
    public function getCache();
}


text:
{% extends 'main/base.html' %}

{% load staticfiles %}

{% block content %}

{% comment %}

Not logged in

You may use the cell counter without logging in, but you will be unable to save any changes to your keyboard map.

Click here to log in.

 {% endcomment %}
{% if not logged_in %}

{% endif %}

Click 'Open Keyboard' to start performing a count.

Press the key corresponding to a cell type to add a count.

To remove a count press 'Backspace+Key'.

To flag a cell as abnormal press 'Space+Key'.

To see example images of the cells in question press 'Shift+Key'.

To see a tabulated differential report close the keyboard. You may also save the report to your local disk.

{% include "main/keyboard_snippet.html" %}

0

    You must have Flash 10 installed to download this file.

    <button class="btn btn-primary" id="downloadify_button" style="width:130px">Save as CSV</button> (this may be opened with Microsoft Excel)

{% comment %}
<form action="" method="POST" class="form-horizontal">
    {% csrf_token %}

    {% include "main/forms/cellcountinstance_form.html" %}
    {% include "main/forms/bonemarrowbackground_form.html" %}
    {% include "main/forms/granulopoiesis_form.html" %}
    {% include "main/forms/erythropoiesis_form.html" %}
    {% include "main/forms/megakaryocytefeatures_form.html" %}
    {% include "main/forms/ironstain_form.html" %}
    {% include "main/forms/overallcomment_form.html" %}

    {% for form in cellcountformslist %}

        {{ form.errors }}
        {{ form.as_p }}

    {% endfor %}

    <input class="btn btn-success" type="submit" value="Submit" />
</form>
{% endcomment %}

At present this site is optimised for Firefox or Chromium-based web browsers, and display sizes of at least 1024x768. See the About section for more information.

{% endblock %}


text:
Aria.beanDefinitions({
    $package : "aria.html.beans.InputElementCfg",
    $description : "Configuration for the generic InputElement.",
    $namespaces : {
        "base" : "aria.html.beans.ElementCfg",
        "common" : "aria.widgetLibs.CommonBeans"
    },
    $beans : {
        "Properties" : {
            $type : "base:Properties",
            $description : "Properties of an InputElement.",
            $properties : {
                "bind" : {
                    $type : "base:Properties.bind",
                    $properties : {
                        "disabled" : {
                            $type : "common:BindingRef",
                            $description : "Binding for the disabled attribute of the input element."
                        }
                    }
                }
            }
        }
    }
});


text:
<?php

namespace Application\Migrations;

use Doctrine\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
class Version20170330122223 extends AbstractMigration
{
    /**
     * @param Schema $schema
     */
    public function up(Schema $schema)
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('ALTER TABLE odp_huurovereenkomsten DROP FOREIGN KEY FK_453FF4A660850352');
        $this->addSql('CREATE TABLE odp_afsluitingen (id INT AUTO_INCREMENT NOT NULL, naam VARCHAR(255) DEFAULT NULL, active TINYINT(1) DEFAULT NULL, created DATETIME NOT NULL, modified DATETIME NOT NULL, discr VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB');
        $this->addSql('ALTER TABLE odp_deelnemers DROP FOREIGN KEY FK_202839997E366551');
        $this->addSql('DROP INDEX IDX_202839997E366551 ON odp_deelnemers');
        $this->addSql('ALTER TABLE odp_deelnemers CHANGE foreign_key klant_id INT NOT NULL');
        $this->addSql('ALTER TABLE odp_deelnemers ADD CONSTRAINT FK_20283999ECDAD1A9 FOREIGN KEY (afsluiting_id) REFERENCES odp_afsluitingen (id)');
        $this->addSql('ALTER TABLE odp_deelnemers ADD CONSTRAINT FK_202839993C427B2F FOREIGN KEY (klant_id) REFERENCES klanten (id)');
        $this->addSql('CREATE INDEX IDX_202839993C427B2F ON odp_deelnemers (klant_id)');
        $this->addSql('ALTER TABLE odp_huurverzoeken ADD afsluiting_id INT DEFAULT NULL, DROP actief, CHANGE einddatum afsluitdatum DATE DEFAULT NULL');
        $this->addSql('ALTER TABLE odp_huurverzoeken ADD CONSTRAINT FK_588F4E96ECDAD1A9 FOREIGN KEY (afsluiting_id) REFERENCES odp_afsluitingen (id)');
        $this->addSql('CREATE INDEX IDX_588F4E96ECDAD1A9 ON odp_huurverzoeken (afsluiting_id)');
        $this->addSql('ALTER TABLE odp_huuraanbiedingen ADD afsluiting_id INT DEFAULT NULL, DROP actief, CHANGE einddatum afsluitdatum DATE DEFAULT NULL');
        $this->addSql('ALTER TABLE odp_huuraanbiedingen ADD CONSTRAINT FK_FA204F87ECDAD1A9 FOREIGN KEY (afsluiting_id) REFERENCES odp_afsluitingen (id)');
        $this->addSql('CREATE INDEX IDX_FA204F87ECDAD1A9 ON odp_huuraanbiedingen (afsluiting_id)');
        $this->addSql('DROP INDEX IDX_453FF4A660850352 ON odp_huurovereenkomsten');
        $this->addSql('ALTER TABLE odp_huurovereenkomsten CHANGE huurovereenkomstafsluiting_id afsluiting_id INT DEFAULT NULL, CHANGE einddatum afsluitdatum DATE DEFAULT NULL');
        $this->addSql('ALTER TABLE odp_huurovereenkomsten ADD CONSTRAINT FK_453FF4A6ECDAD1A9 FOREIGN KEY (afsluiting_id) REFERENCES odp_afsluitingen (id)');
        $this->addSql('CREATE INDEX IDX_453FF4A6ECDAD1A9 ON odp_huurovereenkomsten (afsluiting_id)');
    }

    /**
     * @param Schema $schema
     */
    public function down(Schema $schema)
    {
        $this->throwIrreversibleMigrationException();
    }
}


text:
// Copyright 2014 -  @mobinzk

// ======== Preview Markdown
var Markdown = {
  convert: function($content, $this) {
    $content = "content=" + $content;
    $.ajax({
      url: '/mightycms/markdownHTML',
      type: 'post',
      data: $content,
      dataType: 'html',

      success: function(data){
        // console.log(data);
        $preview = $this.parent().find('.mightycontent');
        $($preview).html(data);
      }
    });
  }
};

var MightyTools = {
    // Initialize Preview
    init: function(){
        // Autoload the preview
        $this = $('textarea');
        $this.each(function(e, input){
            $this = $('#' + input.id);

            // Add tools wysiwyg
            /*jshint multistr: true */
            $this.before('
 \n\

 \n\
                                         \n\

 \n\

 \n\
                                         \n\

 \n\

 \n\
                                         \n\

 \n\

H1
 \n\

H2
 \n\

H3
 \n\

H4
 \n\

H5
 \n\

H6
 \n\

 \n\

 \n\

 \n\
                                         \n\

 \n\

 \n\
                                         \n\

 \n\

 \n\
                                         \n\

 \n\

 \n\
                                         \n\

 \n\

 \n\
                                         \n\

 \n\

');

            // Add mighty Preview
            $height = $this.outerHeight();
            $this.after('
 \n\
                                    Live preview \n\

 \n\

 \n\

');

            // Add the text to mighty Preview
            $thisVal = $this.val();
            if($thisVal) {
                $this.parent().find('.mightypreview').fadeIn('fast');
            }

            $content = $thisVal;
            Markdown.convert($content, $this);
        });
    },
    // Get highlighted text
    highlightedText: function($textarea) {

        var start = 0,
            end = 0,
            normalizedValue,
            range,
            textInputRange,
            len,
            endRange;

            if (typeof $textarea[0].selectionStart == "number" && typeof $textarea[0].selectionEnd == "number") {
                start = $textarea[0].selectionStart;
                end = $textarea[0].selectionEnd;
            } else {
                range = document.selection.createRange();

                if (range && range.parentElement() == el) {
                    len = $textarea[0].value.length;
                    normalizedValue = $textarea[0].value.replace(/\r\n/g, "\n");

                    // Create a working TextRange that lives only in the input
                    textInputRange = $textarea[0].createTextRange();
                    textInputRange.moveToBookmark(range.getBookmark());

                    // Check if the start and end of the selection are at the very end
                    // of the input, since moveStart/moveEnd doesn't return what we want
                    // in those cases
                    endRange = $textarea[0].createTextRange();
                    endRange.collapse(false);

                    if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
                        start = end = len;
                    } else {
                        start = -textInputRange.moveStart("character", -len);
                        start += normalizedValue.slice(0, start).split("\n").length - 1;

                        if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
                            end = len;
                        } else {
                            end = -textInputRange.moveEnd("character", -len);
                            end += normalizedValue.slice(0, end).split("\n").length - 1;
                        }
                    }
                }
            }

        return { start: start, end: end, text: $textarea.val().substring(start, end) };

    },

    // From https://github.com/jamiebicknell/Markdown-Helper
    MarkdownHelper: function (block,event) {

        if(event.keyCode=='13') {
            check = false;
            input = block.value.replace(/\r\n/g,'\n');
            if(block.selectionStart) {
                start = block.selectionStart;
            }
            else {
                block.focus();
                range = document.selection.createRange();
                range.moveStart('character',-input.length);
                start = range.text.replace(/\r\n/g,'\n').length;
            }
            lines = input.split('\n');
            state = input.substr(0,start).split('\n').length;
            value = lines[state-1].replace(/^\s+/,'');
            first = value.substr(0,2);
            if(new RegExp('^[0-9]+[\\.] (.*)$').test(value)) {
                prior = value.substr(0,value.indexOf('. '));
                begin = prior + '. ';
                label = String(eval(prior)+1) + '. ';
                check = true;
            }
            if(value&&!check&&lines[state-1].substr(0,4)=='    ') {
                begin = label = '    ';
                check = true;
            }
            if(['* ','+ ','- ','> '].indexOf(first)>=0) {
                begin = label = first;
                check = true;
            }
            if(check) {
                width = lines[state-1].indexOf(begin);
                if(value.replace(/^\s+/,'')==begin) {
                    block.value = input.substr(0,start-1-width-label.length) + '\n\n' + input.substr(start,input.length);
                    caret = start+1-label.length-width;
                }
                else {
                    block.value = input.substr(0,start) + '\n' + (new Array(width+1).join(' ')) + label + input.substr(start,input.length);
                    caret = start+1+label.length+width;
                }
                if(block.selectionStart) {
                    block.setSelectionRange(caret,caret);
                }
                else {
                    var range = block.createTextRange();
                    range.move('character',caret);
                    range.select();
                }
                return false;
            }
        }
    },
    replaceIt: function(textarea, attr, type) {

        // <:type> is "single", "double", "list", "image", "video", "URL"

        highlighed = MightyTools.highlightedText(textarea);
        text = highlighed.text.trim();
        start = highlighed.start;
        end = highlighed.end;
        len = textarea.val().length;
        attrLength = attr.length;

        textWithTag = textarea.val().substring(start - attrLength, end + attrLength);
        textWith2   = textarea.val().substring(start - 2, end + 2);
        textWith3   = textarea.val().substring(start - 3, end + 3);

        textWit4first  = textarea.val().substring(start - attrLength, end);
        textWit4first2After  = textarea.val().substring(start - attrLength, end + 2);

        textWithhBR    = textarea.val().substring(start - 1, start);
        textWithhBRAfter    = textarea.val().substring(end, end + 1);

        textWithBRListBefore   = textarea.val().substring(start - 2, start);
        textWithBRListAfter    = textarea.val().substring(end, end + 2);

        if(type == 'bold') {
            if(RegExp('^\\*\\*[^]+\\*\\*$').test(textWithTag) ||
               RegExp('^\\*\\*\\*\\*$').test(textWithTag)) {
                textarea.val(textarea.val().substring(0, start - 2) + textWithTag.substring(2, textWithTag.length-2) + textarea.val().substring(end  + 2, len));
                textarea[0].selectionStart = start - 2;
                textarea[0].selectionEnd = end - 2;

            } else {
                textarea.val(textarea.val().substring(0, start) + attr + text + attr + textarea.val().substring(end, len));
                textarea[0].selectionStart = start + attrLength;
                textarea[0].selectionEnd = end + attrLength;
            }

            textarea[0].focus();

        } else if(type == 'italic') {
            if( (RegExp('^\\*[^]+\\*$').test(textWithTag) &&
                 !RegExp('^\\*\\*[^]+\\*\\*$').test(textWith2) ) ||
                 RegExp('^\\*\\*$').test(textWithTag) ||

                 RegExp('^\\*\\*\\*[^]+\\*\\*\\*$').test(textWith3)) {
                textarea.val(textarea.val().substring(0, start - 1) + textWithTag.substring(1, textWithTag.length-1) + textarea.val().substring(end  + 1, len));
                textarea[0].selectionStart = start - 1;
                textarea[0].selectionEnd = end - 1;

            } else {
                textarea.val(textarea.val().substring(0, start) + attr + text + attr + textarea.val().substring(end, len));
                textarea[0].selectionStart = start + attrLength;
                textarea[0].selectionEnd = end + attrLength;
            }

            textarea[0].focus();

        } else if(type == 'h1') {
                if(RegExp('^\n\n#\\s[^]+$').test(textWit4first) || RegExp('^\n\n#\\s$').test(textWit4first)) {
                    textarea.val(textarea.val().substring(0, start - attrLength) + textWit4first2After.substring(4, textWit4first2After.length - 2) + textarea.val().substring(end + 2, len));
                    textarea[0].selectionStart = start - 4;
                    textarea[0].selectionEnd = end - 4;
                } else {
                    textarea.val(textarea.val().substring(0, start) + attr + text + '\n\n' + textarea.val().substring(end, len));
                    textarea[0].selectionStart = start + 4;
                    textarea[0].selectionEnd = end + 4;
                }

        } else if(type == 'h2') {
                if(RegExp('^\n\n##\\s[^]+$').test(textWit4first) || RegExp('^\n\n##\\s$').test(textWit4first)) {
                    textarea.val(textarea.val().substring(0, start - attrLength) + textWit4first2After.substring(5, textWit4first2After.length - 2) + textarea.val().substring(end + 2, len));
                    textarea[0].selectionStart = start - 5;
                    textarea[0].selectionEnd = end - 5;
                } else {
                    textarea.val(textarea.val().substring(0, start) + attr + text + '\n\n' + textarea.val().substring(end, len));
                    textarea[0].selectionStart = start + 5;
                    textarea[0].selectionEnd = end + 5;
                }

        } else if(type == 'h3') {
                if(RegExp('^\n\n###\\s[^]+$').test(textWit4first) || RegExp('^\n\n###\\s$').test(textWit4first)) {
                    textarea.val(textarea.val().substring(0, start - attrLength) + textWit4first2After.substring(6, textWit4first2After.length - 2) + textarea.val().substring(end + 2, len));
                    textarea[0].selectionStart = start - 6;
                    textarea[0].selectionEnd = end - 6;
                } else {
                    textarea.val(textarea.val().substring(0, start) + attr + text + '\n\n' + textarea.val().substring(end, len));
                    textarea[0].selectionStart = start + 6;
                    textarea[0].selectionEnd = end + 6;
                }

        } else if(type == 'h4') {
                if(RegExp('^\n\n####\\s[^]+$').test(textWit4first) || RegExp('^\n\n####\\s$').test(textWit4first)) {
                    textarea.val(textarea.val().substring(0, start - attrLength) + textWit4first2After.substring(7, textWit4first2After.length - 2) + textarea.val().substring(end + 2, len));
                    textarea[0].selectionStart = start - 7;
                    textarea[0].selectionEnd = end - 7;
                } else {
                    textarea.val(textarea.val().substring(0, start) + attr + text + '\n\n' + textarea.val().substring(end, len));
                    textarea[0].selectionStart = start + 7;
                    textarea[0].selectionEnd = end + 7;
                }

        } else if(type == 'h5') {
                if(RegExp('^\n\n#####\\s[^]+$').test(textWit4first) || RegExp('^\n\n#####\\s$').test(textWit4first)) {
                    textarea.val(textarea.val().substring(0, start - attrLength) + textWit4first2After.substring(8, textWit4first2After.length - 2) + textarea.val().substring(end + 2, len));
                    textarea[0].selectionStart = start - 8;
                    textarea[0].selectionEnd = end - 8;
                } else {
                    textarea.val(textarea.val().substring(0, start) + attr + text + '\n\n' + textarea.val().substring(end, len));
                    textarea[0].selectionStart = start + 8;
                    textarea[0].selectionEnd = end + 8;
                }

        } else if(type == 'h6') {
                if(RegExp('^\n\n######\\s[^]+$').test(textWit4first) || RegExp('^\n\n######\\s$').test(textWit4first)) {
                    textarea.val(textarea.val().substring(0, start - attrLength) + textWit4first2After.substring(9, textWit4first2After.length - 2) + textarea.val().substring(end + 2, len));
                    textarea[0].selectionStart = start - 9;
                    textarea[0].selectionEnd = end - 9;
                } else {
                    textarea.val(textarea.val().substring(0, start) + attr + text + '\n\n' + textarea.val().substring(end, len));
                    textarea[0].selectionStart = start + 9;
                    textarea[0].selectionEnd = end + 9;
                }

        } else if(type == 'bullet') {
                if(RegExp('^\n\n-\\s[^]+$').test(textWit4first) || RegExp('^\n\n-\\s$').test(textWit4first)) {
                    textarea.val(textarea.val().substring(0, start - attrLength) + textWit4first2After.substring(4, textWit4first2After.length - 2) + textarea.val().substring(end + 2, len));
                    textarea[0].selectionStart = start - 4;
                    textarea[0].selectionEnd = end - 4;
                } else {
                    textarea.val(textarea.val().substring(0, start) + attr + text + '\n\n' + textarea.val().substring(end, len));
                    textarea[0].selectionStart = start + 4;
                    textarea[0].selectionEnd = end + 4;
                }

                textarea[0].focus();

        } else if(type == 'number') {
                if(RegExp('^\n\n1.\\s[^]+$').test(textWit4first) || RegExp('^\n\n1.\\s$').test(textWit4first)) {
                    textarea.val(textarea.val().substring(0, start - attrLength) + textWit4first2After.substring(5, textWit4first2After.length - 2) + textarea.val().substring(end + 2, len));
                    textarea[0].selectionStart = start - 5;
                    textarea[0].selectionEnd = end - 5;
                } else {
                    textarea.val(textarea.val().substring(0, start) + attr + text + '\n\n' + textarea.val().substring(end, len));
                    textarea[0].selectionStart = start + 5;
                    textarea[0].selectionEnd = end + 5;
                }

                textarea[0].focus();

        } else if(type == 'url') {
             var prompURL = prompt('Please provide the link URL \n\nThis is a link /page/onyour/website within your website. \n\nThis is a link http://www.google.com outside of your website','');
            if(prompURL) {
                textarea.val(textarea.val().substring(0, start) + '[' + text + ']' + '(' + prompURL + ')' + textarea.val().substring(end, len));
            }
        } else if(type == 'image') {
                var prompImage = prompt('Please provide the image URL','');

                if(prompImage) {
                    var prompText = prompt('Please provide the image title (It\'s best to describe the image)','');
                    var prompPosition = prompt('Please provide the image position \n\n(write left or right)','');

                    prompPosition = (prompPosition) ? '{.' + prompPosition + '}' : '';
                    prompText     = (prompText) ? prompText : '';
                    textarea.val(textarea.val().substring(0, start) + '![' + prompText + ']' + '(' + prompImage + ')' + prompPosition + textarea.val().substring(end, len));
                }

        } else if(type == 'video') {
            var prompVideo = prompt('Please provide the video URL from YouTube or Vimeo','');
            var $video;

            if(prompVideo) {
                $video = MightyTools.mediaExtractor(prompVideo);
            }

            if($video) {

                if($video.type == 'youtube') {
                    textarea.val(textarea.val().substring(0, start) + '' + textarea.val().substring(end, len));
                } else if($video.type == 'vimeo') {
                    textarea.val(textarea.val().substring(0, start) + '' + textarea.val().substring(end, len));
                }
            }

        }
    },
    // From http://stackoverflow.com/questions/3452546/javascript-regex-how-to-get-youtube-video-id-from-url
    mediaExtractor: function(pastedData) {
        var success = false;
        var media   = {};

        if (pastedData.match('(http(s)?://)?(www.)?youtube|youtu\.be')) {
            if (pastedData.match('embed')) { youtube_id = pastedData.split(/embed\//)[1].split('"')[0]; }
            else { youtube_id = pastedData.split(/v\/|v=|youtu\.be\//)[1].split(/[?&]/)[0]; }
            media.type  = "youtube";
            media.id    = youtube_id;
            success = true;
        } else if (pastedData.match('(http(s)?://)?(player.)?vimeo\.com')) {
            vimeo_id = pastedData.split(/video\/|http:\/\/vimeo\.com\//)[1].split(/[?&]/)[0];
            media.type  = "vimeo";
            media.id    = vimeo_id;
            success = true;
        }

        if (success) {
            return media;
        } else {
            alert("No valid media id detected");
        }
        return false;
    }
};

$(function(){

    MightyTools.init();

});

// Change preview on keyup
$('html').on('keyup', 'textarea',function(e) {
    $(this).parent().find('.mightypreview').fadeIn('fast');
    $content = escape($(this).val());
    Markdown.convert($content, $(this));
});

// Sort out lists
$('html').on('keypress', 'textarea',function(e) {
    return MightyTools.MarkdownHelper($(this)[0],e);
});

// Scroll at the same time
$('textarea').scroll(function(){
    $this = $(this);
    $preview = $this.parent().find('.mightycontent');
    $preview.scrollTop($this.scrollTop());
});
$('.mightycontent').scroll(function(){
    $this = $(this);
    $preview = $this.parent().parent().find('textarea');
    $preview.scrollTop($this.scrollTop());
});

// Change size on click
$('.uim-form li textarea').click( function(e) {
    $this = $(this);
    $height = $this.outerHeight();

    $preview = $this.parent().find('.mightycontent');
    $preview.css('height', $height);
});

// Mighty tools clicked
$('html').on('click', '.mighty-editor-tools li', function(e){
    textarea = $(this).parent().parent().find('textarea');
    if(textarea.length === 0) {
        textarea = $(this).parent().parent().parent().parent().find('textarea');
    }

    $this = $(this);
    $attr = $this.attr('data-attr');
    $type = $this.attr('data-type');

    MightyTools.replaceIt(textarea, $attr, $type);

    Markdown.convert(textarea.val(), textarea);
});

// ======== Preview Markdown