Harigami
ログイン
anonymous タイトルなし
JavaScript
// 似非Markdownパーサ
/*
  似非Markdownの文法

  # 見出し4
  ## 見出し5
  ### 見出し6

  段落1<br>
  段落1

  段落2
  段落2

  - リスト
  - リスト

  <table>
    <tr></tr>
  </table>

  ※任意のHTML要素を含められるが、トップレベル要素は行頭のスペースが許されない
    さらに、トップレベル要素の開始タグと閉じタグ以外の<が行頭に(スペース無しで)来てはならない
*/

export type DOMDescriptor = {
  tagName: string,
  innerHTML?: string,
  childNodes?: DOMDescriptor[],
};

type OrdinalLineType = 'H4' | 'H5' | 'H6' | 'LI' | 'EMPTY' | 'NORMAL';
type DOMLineType = 'ESTART' | 'EEND' | 'ESTANDALONE';
type LineType = OrdinalLineType | DOMLineType;
type OrdinalLineDescriptor = {
  type: LineType,
  content: string,
};
type DOMLineDescriptor = {
  type: DOMLineType,
  tagName: string,
  content: string,
};
type LineDescriptor = OrdinalLineDescriptor | DOMLineDescriptor;

export function line2desc(line: string): LineDescriptor {
  let mtc: RegExpMatchArray | null;
  if (line[0] === '#') {
    if ((mtc = line.match(/^# (.*)$/))) return { type: 'H4', content: mtc[1] };
    if ((mtc = line.match(/^## (.*)$/))) return { type: 'H5', content: mtc[1] };
    if ((mtc = line.match(/^### (.*)$/))) return { type: 'H6', content: mtc[1] };
  }
  if (line[0] === '-') {
    if ((mtc = line.match(/^- (.*)$/))) return { type: 'LI', content: mtc[1] };
  }
  if (line[0] === '<') {
    if ((mtc = line.match(/^<([a-zA-Z0-9]+)(?:".*?"|'.*?'|[^'"])*?>/))) {
      if (mtc[1].match(/br|img|hr|input/i)) return { type: 'ESTANDALONE', tagName: mtc[1].toUpperCase(), content: line };
      else return { type: 'ESTART', tagName: mtc[1].toUpperCase(), content: line };
    }
    if ((mtc = line.match(/^<\/([a-zA-Z0-9]+)>/))) return { type: 'EEND', tagName: mtc[1].toUpperCase(), content: line };
  }
  if (line[0] === '-' && (mtc = line.match(/^- (.*)$/))) return { type: 'LI', content: mtc[1] };
  if (line.match(/^\s*$/)) return { type: 'EMPTY', content: line };
  return { type: 'NORMAL', content: line };
}

function desc2raw(line: LineDescriptor): string {
  switch (line.type) {
    case 'H4': return '# ' + line.content;
    case 'H5': return '## ' + line.content;
    case 'H6': return '### ' + line.content;
    case 'LI': return '- ' + line.content;
    default: return line.content;
  }
}

class MarkdownParser {
  private q = 0; // 0: 初期状態, 1: 段落読み取り, 2: リスト読み取り, 3: DOM読み取り
  private readonly text: string;
  private html: string | null = null;

  constructor(text: string) {
    this.text = text;
  }

  public toHTML(): string {
    if (this.html) return this.html;
    this.html = '';

    let idx = 0;
    const lines = this.text.split('\n').map(line2desc);
    while (idx in lines) {
      if (this.consume(lines[idx])) idx++;
    }
    this.consume({ type: 'EMPTY', content: '' });

    return this.html;
  }

  private consume(desc: LineDescriptor): boolean {
    switch (this.q) {
      case 0: return this.onNormal(desc);
      case 1: return this.onPara(desc);
      case 2: return this.onList(desc);
      case 3: return this.onDOM(desc);
      default: throw new Error('Illegal State');
    }
  }

  private onNormal(line: LineDescriptor): boolean {
    switch (line.type) {
      case 'H4':
      case 'H5':
      case 'H6':
        this.html = this.html + `<${line.type}>${line.content}</${line.type}>`;
        return true;
      case 'NORMAL':
        this.html = this.html + '<P>';
        this.q = 1;
        return false;
      case 'LI':
        this.html = this.html + '<UL>';
        this.q = 2;
        return false;
      case 'EMPTY':
        return true;
      case 'ESTANDALONE':
        this.html = this.html + line.content + '\n';
        return true;
      case 'ESTART':
        this.q = 3;
        return false;
      case 'EEND':
        throw new Error('Parse Error(Unexpected EEND)');
    }
  }

  private onPara(line: LineDescriptor): boolean {
    switch (line.type) {
      case 'NORMAL':
        this.html = this.html + line.content + ' ';
        return true;
      default:
        this.html = this.html!.substr(0, this.html!.length - 1);
        this.html = this.html + '</P>';
        this.q = 0;
        return false;
      case 'EEND':
        throw new Error('Parse Error(Unexpected EEND)');
    }
  }

  private onList(line: LineDescriptor): boolean {
    switch (line.type) {
      case 'LI':
        this.html = this.html + `<LI>${line.content}</LI>`;
        return true;
      default:
        this.html = this.html + '</UL>';
        this.q = 0;
        return false;
      case 'EEND':
        throw new Error('Parse Error(Unexpected EEND)');
    }
  }

  private onDOM(line: LineDescriptor): boolean {
    switch (line.type) {
      default:
        this.html = this.html + desc2raw(line) + '\n';
        return true;
      case 'EEND':
        this.html = this.html + line.content + '\n';
        this.q = 0;
        return true;
    }
  }
}

export function md2html(md: string) {
  const parser = new MarkdownParser(md);
  return parser.toHTML();
}
  • 0
  • 1
anonymous タイトルなし
JavaScript
console.log("thank you!!!")
  • 0
  • 0
anonymous タイトルなし
JavaScript
console.log("hello world")
  • 1
  • 0
anonymous タイトルなし
JavaScript
/*雲除去の関数定義*/

function cloudMasking(image) {
  var qa = image.select('QA60');
  /*
  Bits 10 : dense(opaque) cloud
  Bits 11 : cirrus cloud
  */
  
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;
  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      .and(qa.bitwiseAnd(cirrusBitMask).eq(0));
  return image.updateMask(mask).divide(10000);
}

var point = ee.Geometry.Point([138.1075,37.09666]);
Map.centerObject(point,17);


/*after*/
var start2 = ee.Date('2019-11-02');
var finish2 = ee.Date('2019-11-03');

var images2 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterDate(start2, finish2)
.filterBounds(point);
//print(images2.size());

var image2b = ee.Image(images2.first());
var image2 = cloudMasking(image2b);

var truecolor2 = {bands:["B2","B3","B4"],min:0,max:0.3};
Map.addLayer(image2,truecolor2,'truecolor2');


//B8:NIR B4:R
var ndvi = image2.normalizedDifference(["B8","B4"])
.rename('NDVI2');

var landobs = {
bands:['NDVI2'],
min:0.0, max:1.0,
palette:['FFFFFF','CE7E45','DF923D','F1B555', 
           'FCD163','99B718','74A901','66A000', 
           '529400','3E8601','207401','056201',
           '004C00','023B01','012E01','011D01', 
           '011301']};


Map.addLayer(ndvi,landobs,'NDVI2');

var check_palette = ee.Algorithms.If(ndvi<0.4, ['00FF00'], ['FF0000']);

var land = {
bands:['NDVI2'],
min:0.0, max:1.0,
palette : check_palette};

Map.addLayer(ndvi,land,'check');
  • 0
  • 0
anonymous タイトルなし
JavaScript
/*雲除去の関数定義*/

function cloudMasking(image) {
  var qa = image.select('QA60');
  /*
  Bits 10 : dense(opaque) cloud
  Bits 11 : cirrus cloud
  */
  
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;
  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      .and(qa.bitwiseAnd(cirrusBitMask).eq(0));
  return image.updateMask(mask).divide(10000);
}

/*主コード*/

var point = ee.Geometry.Point([138.1075,37.09666]);
Map.centerObject(point,17);

/*before*/
var start1 = ee.Date('2019-09-08');
var finish1 = ee.Date('2019-09-09');

var images1 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterDate(start1, finish1)
.filterBounds(point);
//print(images1.size());

var image1b = ee.Image(images1.first());
var image1 = cloudMasking(image1b);

var truecolor1 = {bands:["B2","B3","B4"],min:0,max:0.3};
Map.addLayer(image1,truecolor1,'truecolor1');


//B8:NIR B4:R
var ndvi = image1.normalizedDifference(["B8","B4"])
.rename('NDVI1');
var landobs = {
bands:['NDVI1'],
min:0.0, max:1.0,
palette:['FFFFFF','CE7E45','DF923D','F1B555', 
           'FCD163','99B718','74A901','66A000', 
           '529400','3E8601','207401','056201',
           '004C00','023B01','012E01','011D01', 
           '011301']};
Map.addLayer(ndvi,landobs,'NDVI1');


/*after*/
var start2 = ee.Date('2019-11-02');
var finish2 = ee.Date('2019-11-03');

var images2 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterDate(start2, finish2)
.filterBounds(point);
//print(images2.size());

var image2b = ee.Image(images2.first());
var image2 = cloudMasking(image2b);

var truecolor2 = {bands:["B2","B3","B4"],min:0,max:0.3};
Map.addLayer(image2,truecolor2,'truecolor2');


//B8:NIR B4:R
var ndvi = image2.normalizedDifference(["B8","B4"])
.rename('NDVI2');
var landobs = {
bands:['NDVI2'],
min:0.0, max:1.0,
palette:['FFFFFF','CE7E45','DF923D','F1B555', 
           'FCD163','99B718','74A901','66A000', 
           '529400','3E8601','207401','056201',
           '004C00','023B01','012E01','011D01', 
           '011301']};
Map.addLayer(ndvi,landobs,'NDVI2');
  • 0
  • 0
anonymous タイトルなし
JavaScript
/*主コード*/

var point = ee.Geometry.Point([138.1075,37.09666]);
Map.centerObject(point,17);

/*before*/
var start1 = ee.Date('2019-09-08');
var finish1 = ee.Date('2019-09-09');

var images1 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterDate(start1, finish1)
.filterBounds(point);
//print(images1.size());

var image1 = ee.Image(images1.first());

var truecolor1 = {bands:["B2","B3","B4"],min:0,max:3000};
Map.addLayer(image1,truecolor1,'truecolor1');


//B8:NIR B4:R
var ndvi = image1.normalizedDifference(["B8","B4"])
.rename('NDVI1');
var landobs = {
bands:['NDVI1'],
min:0.0, max:1.0,
palette:['FFFFFF','CE7E45','DF923D','F1B555', 
           'FCD163','99B718','74A901','66A000', 
           '529400','3E8601','207401','056201',
           '004C00','023B01','012E01','011D01', 
           '011301']};
Map.addLayer(ndvi,landobs,'NDVI1');


/*after*/
var start2 = ee.Date('2019-11-02');
var finish2 = ee.Date('2019-11-03');

var images2 = ee.ImageCollection('COPERNICUS/S2_SR')
.filterDate(start2, finish2)
.filterBounds(point);
//print(images2.size());

var image2 = ee.Image(images2.first());

var truecolor2 = {bands:["B2","B3","B4"],min:0,max:3000};
Map.addLayer(image2,truecolor2,'truecolor2');


//B8:NIR B4:R
var ndvi = image2.normalizedDifference(["B8","B4"])
.rename('NDVI2');
var landobs = {
bands:['NDVI2'],
min:0.0, max:1.0,
palette:['FFFFFF','CE7E45','DF923D','F1B555', 
           'FCD163','99B718','74A901','66A000', 
           '529400','3E8601','207401','056201',
           '004C00','023B01','012E01','011D01', 
           '011301']};
Map.addLayer(ndvi,landobs,'NDVI2');
  • 0
  • 0
anonymous タイトルなし
JavaScript
var point = ee.Geometry.Point([138.25,37.15]);
//var point = ee.Geometry.Point([138,35.15]);
Map.centerObject(point,11);

var start = ee.Date('2019-10-11');
var finish = ee.Date('2019-10-19');

var images = ee.ImageCollection('COPERNICUS/S2_SR')
.filterDate(start, finish)
.filterBounds(point);

var image = ee.Image(images.first());

print(images.size());
//B8:NIR B4:R
var truecolor = {bands:["B2","B3","B4"],min:0,max:5000};
Map.addLayer(image,truecolor,'truecolor');

var ndvi = image.normalizedDifference(["B8","B4"])
.rename('NDVI');
var landobs = {
bands:['NDVI'],
min:0.0, max:1.0,
palette:['0000FF','000000','00FF00']};
Map.addLayer(ndvi,landobs,'NDVI');
  • 0
  • 0
anonymous タイトルなし
JavaScript
tests
  • 0
  • 0
anonymous タイトルなし
JavaScript
javascript:(function(){(function(){[{color:"#F44336",selector:"h1"},{color:"#E91E63",selector:"h2"},{color:"#9C27B0",selector:"h3"},{color:"#673AB7",selector:"h4"},{color:"#3F51B5",selector:"h5"},{color:"#2196F3",selector:"h6"}].map(function(a){for(var c=document.querySelectorAll(a.selector),b=0,f=c.length;b<f;++b){var d=c[b];d.style.backgroundColor=a.color;var e=document.createElement("span");e.innerHTML=" | "+a.selector;d.appendChild(e)}})})();})();
  • 0
  • 0
anonymous タイトルなし
JavaScript
<!DOCTYPE html>
<html lang="ja">
    <head charset="utf-8">
        <title>JS Practice</title>
        <style>
            .box{
                width: 100px;
                height: 100px;
                background-color: skyblue;
                cursor: pointer;
                transition: 1.2s;
            }
            .circle{
                background-color: pink;
                border-radius: 50%;
                transform: rotate(360deg);
            }
        </style>
    </head>
    <body>
        <div class="box" id="target"></div>
        <script>
            'use strict';

            const target = document.getElementById('target');

            target.addEventListener('click', () => {
                target.classList.toggle('circle');
            });
        </script>
    </body>
</html>
  • 0
  • 1
あなたもコードを投稿しませんか?
投稿する